I am opening a QGIS project in a stand-alone Python script, but the existing mapLayers are not being found. The script is below. I am using PyQGIS 3.4 and I haven't been successful at using other solutions posted to the web for older versions of the API.

from qgis.core import *
from pprint import pprint

if __name__ == '__main__':
    import sys

    project_name = sys.argv[1]

    QgsApplication.setPrefixPath('/usr', True)
    qgs = QgsApplication([], False)
    project = QgsProject.instance()

If I use the Python console in the QGIS application. Then I can see this:

>>> from pprint import pprint
>>> QgsProject.instance().read('/home/janderso/code/data/audi/audi_debug/agg-545/audi_southbound.qgs')
>>> pprint(QgsProject.instance().mapLayers())
{'lane_marking_lateral_edges_041b777d_5e98_478d_96e6_ed3bf590a7a4': <qgis._core.QgsVectorLayer object at 0x7f710d7f4e58>,
'lane_marking_longitudinal_edges_be9d3f0b_ed83_4191_a0ee_1413209985b6': <qgis._core.QgsVectorLayer object at 0x7f710d7f4ee8>,
'lane_marking_nodes_29a65426_e9d8_4b8e_a74b_582ed3223e1d': <qgis._core.QgsVectorLayer object at 0x7f710d7f4f78>,
'road_boundary_lateral_edges_4f6c6b66_5d4c_4e3d_bb2c_d538215701ff': <qgis._core.QgsVectorLayer object at 0x7f7181f0e048>,
'road_boundary_longitudinal_edges_c0489433_be82_401a_88f4_8875e06025cc': <qgis._core.QgsVectorLayer object at 0x7f7181f0e0d8>,
'road_boundary_nodes_9fbd6dd6_69e8_420e_a126_e22cfd279df2': <qgis._core.QgsVectorLayer object at 0x7f7181f0e168>,
'sign_nodes_01febdb7_a31c_4f55_9f36_9ca71a077835': <qgis._core.QgsVectorLayer object at 0x7f7181f0e1f8>,
'sign_sign_edges_32c2531d_89bc_4046_8b2b_8d355920509a': <qgis._core.QgsVectorLayer object at 0x7f7181f0e288>}

Does anyone know the correct way to load an existing project using PyQGIS 3.4?


Try this little chunk in your Python Console. It worked for me.

layerList = iface.mapCanvas().layers()
for layer in layerList:
    name = layer.name()
  • Sorry - I just re-read your post about a standalone script. This may not work for you. – Cary H Dec 21 '18 at 19:10

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.