2

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?

3
  • 2
    Add r prefix to prefix path -> r"C:\Program........"
    – Noura
    Mar 25, 2020 at 10:31
  • That does not help unfortunately
    – Till B
    Mar 25, 2020 at 10:34
  • I can work with the layers dict, but rendering (which is my main point) does not work. It does work from inside the QGIS application, though.
    – Till B
    Mar 25, 2020 at 10:36

1 Answer 1

0

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.10\bin directory there was a sample batch file to set the environment variables, which did so in the correct order, such that it worked.

3
  • However a follow-up question arises, which I will post separately.
    – Till B
    Mar 25, 2020 at 13:44
  • I am facing the same problem. Could you add some more details to your answer? Which batch file are you referring to? Did you simply call the batch file before starting the Python interpreter, and then everything worked?
    – Jake
    Apr 4, 2020 at 19:56
  • Hi @Jake, there is a batch file template in the QGIS3.10\bin folder, called python-qgis.bat. It sets a lot of environment variables and then starts the python installation that shipped with QGIS (and not system Python). I did a lot of trial and error to get my setup work the way I wanted, but in the end I ended up installing all packages that I needed into the QGIS Python and using that. It is a bit clumsy to use because you have to set all the variables first, but it works
    – Till B
    Apr 6, 2020 at 6:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.