My goal is to create a standalone PyQGIS script that is able to run QgsProcessingAlgorithm
scripts without GUI. My setup runs on Windows and my IDE of choice is VS Code.
I've followed the Windows instructions from the official documentation and added those environment variables to my system.
I'd like to be able to simply run the following two lines from VS Code:
from qgis.core import *
import processing
without getting into problems. This looks easy, right? Wrong. First thing you run into when running this code is that it is missing PyQt.
No module named 'PyQt5.QtCore'
There is no mention of this in the documentation, so naturally you might be inclined to think: okay, let's install that using pip
.
Now the first line from qgis.core import *
runs fine, but the second doesn't.
No module named 'processing'
To let this work, apparently a lot more environment paths (again undocumented) need to set. So with the help of @Kadir's answer I created the following batch file:
SET OSGEO4W_ROOT=C:\OSGeo4W
call %OSGEO4W_ROOT%\bin\o4w_env.bat
path %PATH%;%OSGEO4W_ROOT%\apps\qgis\bin
path %PATH%;%OSGEO4W_ROOT%\apps\Qt5\bin
path %PATH%;%OSGEO4W_ROOT%\apps\Python39\Scripts
set QGIS_PREFIX=%OSGEO4W_ROOT%\apps\qgis
set PYTHONHOME=%OSGEO4W_ROOT%\apps\Python39
set PYTHONPATH=%OSGEO4W_ROOT%\apps\qgis\python;%OSGEO4W_ROOT%\apps\qgis\python\plugins;%PYTHONPATH%
set QT_QPA_PLATFORM_PLUGIN_PATH=%OSGEO4W_ROOT%\apps\Qt5\plugins\platforms
set QT_PLUGIN_PATH=%OSGEO4W_ROOT%\apps\qgis\qtplugins;%OSGEO4W_ROOT%\apps\qt5\plugins
set GDAL_FILENAME_IS_UTF8=YES
set VSI_CACHE=TRUE
set VSI_CACHE_SIZE=1000000
Next I restarted VS Code and executed the initial two lines again. But immediately got into problems. This time PyQT complains:
qgis._gui cannot import type '����' from PyQt5.QtCore
After looking at various posts here and reported issues on GitHub, it turns out that that error represents a mismatch between the just installed PyQt5, and the one that is apparently shipped with QGIS itself.
Long story short: don't use pip
to install PyQt5. So I uninstalled it and retried. Unfortunately I once again got the error:
No module named 'PyQt5.QtCore'
even after running that batch file. So I found another batch file, but that one didn't help either. I ended up with the same No module named 'PyQt.QtCore'
error and after fiddling with combining the two batch files I even managed to get this error:
No module named 'osgeo'
somewhere deep in the import processing
line. And that's when I concluded that solving this problem is way beyond my capabilities.
Would someone be able to write a step-by-step instruction on how to let those two lines of code run just fine in VS Code, from a fresh installation of QGIS?
import processing
, all went downhill as described above. Yes I tested my paths, and verified them by calling 'set' from the commandline, before opening VS Code. I reinstalled OSGeo4W using the default settings. Are there perhaps any settings in the installer that I forgot to enable? I'll uninstall all again and investigate the settings.json solution offered in that post, thanks for this.