I am trying to use the QGIS python API to read out the groups and layers in a QGIS (2.4) project. Here is a code snippet from my script, which is run from a shell, not from the QGIS python console!

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


print "QGis version:"+QGis.QGIS_VERSION
print "Project title: "+proj.title()
print "Project tree root children:"
for node in root.children():
    print "- "+node.name()+" ("+str(type(node))+")"

It prints the following when I run it:

QGis version:2.4.0-Chugiak
Project title: This is my project title
Project tree root children:
- Field Data (<class 'qgis._core.QgsLayerTreeGroup'>)
- Aerial Photos (<class 'qgis._core.QgsLayerTreeGroup'>)
- Other Maps (<class 'qgis._core.QgsLayerTreeGroup'>)
- Model Results (<class 'qgis._core.QgsLayerTreeGroup'>)

Only QgsLayerTreeGroup objects are returned but no layers (QgsLayerTreeLayer objects), even though I also have two layers in the project root. When I open the python console in QGIS while I have my project open and run:

[<qgis._core.QgsLayerTreeGroup object at 0xa3f9170>, <qgis._core.QgsLayerTreeGroup object at 0xa3f9200>, <qgis._core.QgsLayerTreeGroup object at 0xa3f9290>, <qgis._core.QgsLayerTreeGroup object at 0xa3f9320>, <qgis._core.QgsLayerTreeLayer object at 0xa3fe320>, <qgis._core.QgsLayerTreeLayer object at 0xa3fe830>]

This does return the QgsLayerTreeLayer objects for my layers that I'm after. How come there is a difference?

Edit: I'm trying to run my python script from a shell, not from the QGIS python console.

2 Answers 2


It turned out I did not properly initialize QGis like you're supposed to when writing standalone scripts. Adding the following lines made everything work:

QgsApplication([], True) #a path was not needed

#do all the things with your QgsProject here


This question and the PyQGIS Developer Cookbook got me on the right track.


look at Martin Dobias's QGIS Layer Tree API (Part 1)

from PyQt4.QtCore import QFileInfo # no need of from qgis.core import * in the console)
for child in root.children():
    if isinstance(child, QgsLayerTreeGroup):
         print "- group: " ,child.name()
    elif isinstance(child, QgsLayerTreeLayer):
         print "-layer: ", child.layerName()
  • Thanks for your answer. Sorry, I meant to say I'm trying to run my script from a shell, not from the QGIS console. I think I understand how the code works (I found Martin's posts as well) but for some reason in my case root.children() only returns QgsLayerTreeGroup objects and no QgsLayerTreeLayer objects, even though I clearly also have layers in my project.
    – user39235
    Oct 16, 2014 at 11:08
  • No problem for me from the shell. What is your QGIS version ?
    – gene
    Oct 16, 2014 at 18:25

Your Answer

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

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