Thursday, 18 January 2018

pyqgis - Importing paths for QGIS 3 standalone scripts


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 "", line 1, in
File "C:\Program Files\QGIS 3.0\apps\qgis\python\qgis\__init__.py", line 26, in
from builtins import zip
ImportError: No module named builtins



How can I set the paths correctly?




Answer



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" %*

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...