10

I'm trying to load an existing .qgs project and get a list of the layers inside. I seem to be able to open the project, but I can't get the list of layers. I'm a little confused as to the basic PyQGIS syntax, and the cookbook doesn't cover this specific example.

from qgis.core import *
from PyQt4.QtCore import QFileInfo


QgsApplication.setPrefixPath(r"C:\OSGeo4W\apps\qgis-ltr", True)
qgs = QgsApplication([], False)
qgs.initQgis()
# Get the project instance
project = QgsProject.instance()
# Open the project
project.read(QFileInfo(
        r'C:\path\to\project\project.qgs'))
print project.fileName()
# Get the layers in the project
layers = QgsMapLayerRegistry.instance().mapLayers()

print layers
qgs.exitQgis()

Currently, this just layers returns an empty dict, despite the fact that the project exists and has many layers in it.

5
  • I have the same problem in QGIS 2.18.2 tried steps of OP and @GermánCarrillo. What's OP's version?
    – raphael
    Dec 28 '16 at 18:47
  • I was using QGIS 2.16.2 on Windows 7x64 at the time. Have since tried on 2.18.2 and still did not work.
    – KJYDavis
    Dec 28 '16 at 20:35
  • I flagged this question for reopening because the linked question applies to within QGIS, but the solution doesn't work in a standalone Python script
    – raphael
    Dec 30 '16 at 18:36
  • Have you tried: layers = QgsMapLayerRegistry.instance().mapLayers().keys()
    – Albert
    Jan 4 '17 at 15:42
  • Can you try to insert QgsApplication.processEvents() right after project.read? Jan 5 '17 at 7:23
10
+50

I also receive an empty dictionary when I run your code but the following works for me which is slightly different:

from qgis.core import *
from PyQt4.QtCore import QFileInfo
from PyQt4.QtGui import QApplication
import os

from os.path import expanduser
home = expanduser("~")

QgsApplication( [], False, home + "/AppData/Local/Temp" )
QgsApplication.setPrefixPath("C:/OSGeo4W64/apps/qgis", True)
app = QApplication([], True)
QgsApplication.initQgis()

# Get the project instance
project = QgsProject.instance()
# Open the project
project.read(QFileInfo('C:\path\to\project\project.qgs'))
print project.fileName()
# Get the layers in the project
layers = QgsMapLayerRegistry.instance().mapLayers()
print layers

QgsApplication.exitQgis()
app.exit()

Tested on QGIS 2.18.3 for Windows 7 64-bit.

Result



Edit:

The main difference, I believe, between your code and what I used is that you need to create the QApplication object before creating the QgsApplication. So you would need to replace:

qgs = QgsApplication([], False)

with this:

qgs = QApplication([], False)

But the QApplication class needs to be imported so we must add the following:

from PyQt4.QtGui import QApplication

And finally, if you want to do a cleanup, you need to add the following at the end.

QgsApplication.exitQgis()

So you could try using the following code which is more close to your original code:

from qgis.core import *
from PyQt4.QtCore import QFileInfo
from PyQt4.QtGui import QApplication

QgsApplication.setPrefixPath("C:/OSGeo4W64/apps/qgis", True)
qgs = QApplication([], False)
QgsApplication.initQgis()
# Get the project instance
project = QgsProject.instance()
# Open the project
project.read(QFileInfo('C:/path/to/project/project.qgs'))
print project.fileName()
# Get the layers in the project
layers = QgsMapLayerRegistry.instance().mapLayers()

print layers
QgsApplication.exitQgis()
4
  • This worked on Win7x64 QGIS 2.8.6 32-bit. I'll check other versions later in the week.
    – KJYDavis
    Jan 4 '17 at 18:33
  • 1
    @Joseph, could you explain what's different?
    – raphael
    Jan 4 '17 at 21:14
  • @raphael - Thanks, I've edited the post to include the main differences ;)
    – Joseph
    Jan 5 '17 at 10:11
  • 1
    Also worked on Win7x64 QGIS 2.14.9x64
    – raphael
    Jan 5 '17 at 19:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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