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?
Best 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:
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: