TransWikia.com

Errors while importing QGIS 3.10 from standalone Python, but not from QGIS's Python Console

Geographic Information Systems Asked by Till B on June 14, 2021

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 FilesQGIS 3.10appsqgis", 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 FilesQGIS 3.10binpython3.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/python3profilesdefault/projproj.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?

One Answer

Ok, I found the answer myself. The order of setting system variables was not correct. In particular the environment variables for Qt were not set correctly, because they were partly overwritten. In the QGIS 3.10bin directory there was a sample batch file to set the environment variables, which did so in the correct order, such that it worked.

Answered by Till B on June 14, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP