I try to load a QGIS project in standalone Python on Windows 10. This is the code that I use:
from qgis.core import *
qgs = QgsApplication([], False)
QgsApplication.setPrefixPath("C:\Program Files\QGIS 3.10\apps\qgis", True)
QgsApplication.initQgis()
project = QgsProject.instance()
project.read("filename.qgz")
layers = project.mapLayers()
print(layers)
Running this code from the Python console inside the QGIS application works perfectly and it returns a dict of the layers in the project.
However my goal is to build a standalone Python script. I use the Python interpreter that came with the QGIS installation (C:\Program Files\QGIS 3.10\bin\python3.exe
) for this. The environment variables are set correctly using e.g. a modification of this description on how to set them. I also double checked that the variables inside the QGIS application and in the command line are the same. Now when executing the above code with the standalone Python interpreter, I get this error message:
Logged warning: Loading a file that was saved with an older version of qgis (saved in 3.4.8-Madeira, loaded in 3.10.2-A Coru?a). Problems may occur.
pj_open_lib(proj.db): call fopen(C:/Users/ESPPC25/AppData/Roaming/python3\profiles\default/proj\proj.db) - succeeded
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
QObject::connect: invalid null parameter
qt.svg: Cannot open file ':/images/composer/missing_image.svg', because: No such file or directory
qt.svg: Cannot open file ':/images/composer/missing_image.svg', because: No such file or directory
qt.svg: Cannot open file ':/images/composer/missing_image.svg', because: No such file or directory
qt.svg: Cannot open file ':/images/composer/missing_image.svg', because: No such file or directory
qt.svg: Cannot open file ':/images/composer/missing_image.svg', because: No such file or directory
qt.svg: Cannot open file ':/images/composer/missing_image.svg', because: No such file or directory
qt.svg: Cannot open file ':/images/composer/missing_image.svg', because: No such file or directory
qt.svg: Cannot open file ':/images/composer/missing_image.svg', because: No such file or directory
qt.svg: Cannot open file ':/images/composer/missing_image.svg', because: No such file or directory
QThreadStorage: Thread 0x21e690e9c30 exited after QThreadStorage 4 destroyed
QWaitCondition: Destroyed while threads are still waiting
I can still execute methods like layers.keys()
, put printing anything from the layer dict results in the above error message.
What is going on? How can I get the layers from my project in standalone Python? What else could be different between the Python console inside the QGIS application and the standalone Python interpreter?
r
prefix to prefix path ->r"C:\Program........"
layers
dict, but rendering (which is my main point) does not work. It does work from inside the QGIS application, though.