5

For QGIS 2.x, I used the batch file provided in the answer for this post in the OSGeo4W Shell to run standalone scripts: Problem with import qgis.core when writing a stand-alone PyQGIS script

The batch file for QGIS 2.18.17-ltr looks like this:

set PYTHONPATH=C:\Program Files\QGIS 2.18\apps\qgis-ltr\python
set PATH=C:\Program Files\QGIS 2.18\apps\qgis-ltr\bin;%PATH%

Now after installing QGIS 3.0, I would also like to run some standalone scripts. So I tweaked the above batch file:

set PYTHONPATH=C:\Program Files\QGIS 3.0\apps\qgis\python\
set PATH=C:\Program Files\QGIS 3.0\apps\qgis\bin;%PATH%

However, when I load this batch file into the shell and use from qgis.core import *, I receive the following error message:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Program Files\QGIS 3.0\apps\qgis\python\qgis\__init__.py", line 26, in <module>
    from builtins import zip
ImportError: No module named builtins 

How can I set the paths correctly?

Joseph
  • 75,746
  • 7
  • 171
  • 282
  • 2
    builtins module is new to python3 IIRC. Have you tried running python3 instead of python, it's possible you may be running python 3 code with a python 2 interpreter? – Steven Kay Mar 23 '18 at 15:00
  • 1
    There is python template batch file in osgeo4w/bin. I think you can just use it to call python3 or take it to call a IDE. – Andreas Müller Mar 23 '18 at 22:34
  • @AndreasMüller - Thank you, you are correct in that there is a similar batch file for the standalone QGIS application in QGIS 3.0/bin/python-qgis.bat. Running this sets the paths correctly and can execute the scripts. Please post your comment as an answer :) – Joseph Mar 26 '18 at 10:05
  • @StevenKay - Thank you, you were right about me using Python 2.7 instead of 3. There is a batch file which can set the paths accordingly as mentioned by Andreas =) – Joseph Mar 26 '18 at 10:06

1 Answers1

10

For QGIS3 we need a special environment, that differs from the QGIS2-Versions. Because not only Python changed to python3 but also pyqt5/Qt5 need different settings. To work with a complete environment for QGIS3/python3-Development we can use a batch-template file in the folder osgeo4w\bin, it is called python-qgis.bat.tmpl.

Here I shortly explain the content:

  • call "%~dp0\o4w_env.bat" executes the file o4w_env.bat, containing the basic environment for OSGEO4W-Programms. Mainly it executes further batch-files in osgeo4w\etc\ini and creates the most important variable OSGEO4W_ROOT.
  • call qt5_env.bat sets the environment variables for using the Qt5-Widget library. QGIS3 also needs this to start! It's separated from the first batch file, because QGIS2 does work with Qt4, which settings are different!
  • py3_env.bat is executed to set python3 specific variables and we have a separated batch, because it differs from python2. So we call this one, if we need a python3-environment with PYTHONHOME and PYTHONPATH. The rest of the file is to "connect" Python3 with QGIS3. Important elements are an exteded PATH, the QGIS_PREFIX_PATH, QT_PLUGIN_PATH and the extend for PYTHONPATH. This guaranties to build QGIS-Plugins and standalone applications.

Though the easiest way to call a standalone-script is to use this template. Just remove the file extension .tmpl and use it.

If we don't want to call python.exe but a some Python-IDE we can use this template, too. Here we change just the last line of the script, to start the IDE.

The batch file content looks like this:

@echo off
call "%~dp0\o4w_env.bat"
call qt5_env.bat
call py3_env.bat
@echo off
path %OSGEO4W_ROOT%\apps\qgis\bin;%PATH%
set QGIS_PREFIX_PATH=%OSGEO4W_ROOT:\=/%/apps/qgis
set GDAL_FILENAME_IS_UTF8=YES
rem Set VSI cache to be used as buffer, see #6448
set VSI_CACHE=TRUE
set VSI_CACHE_SIZE=1000000
set QT_PLUGIN_PATH=%OSGEO4W_ROOT%\apps\qgis\qtplugins;%OSGEO4W_ROOT\apps\qt5\plugins
set PYTHONPATH=%OSGEO4W_ROOT%\apps\qgis\python;%PYTHONPATH%
"%PYTHONHOME%\python" %*
Andreas Müller
  • 2,622
  • 13
  • 20
  • what is the last line looks like if we want to run the script through sublime text? – Ayato Jul 04 '18 at 16:25
  • I don't know sublime, but as it is an editor, i guess you should run this script instead of python.exe. Is there a possibility to config an external command? – Andreas Müller Jul 04 '18 at 16:48
  • oh I see. Is this what you mean by config an external command? Oh, one more question. If I move the bat file to another directory, it didn't work, what change should I make to the bat file in order to work? – Ayato Jul 05 '18 at 01:43
  • I was just guessing, how sublime may work, so i really can't tell. If you want to put the bat file into another directory you have to hard code the path pointing to o4w_env.bat, replacing %~dp0 by C:\Osgeo4w\bin for example. %~dp0 means just the name of the directory where the file is stored... – Andreas Müller Jul 05 '18 at 06:28
  • Has this changed for QGIS 3.22? it can't find qt5_env.bat in C:\OSGeo4W. qgis-dev.bat is very different now. – GeorgeC Dec 14 '21 at 05:05
  • If you are using the new installer: yes. Calling all the batch files in etc/ini with call "%~dp0\o4w_env.bat" now executes py3_env and qt5_env. – Andreas Müller Dec 14 '21 at 09:25