4

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)
    QgsApplication.setThemeName('default')
    qgs = QgsApplication([], False)
    qgs.initQgis()
    print(qgs.showSettings())
    project = QgsProject.instance()
    project.read(project_name)
    pprint(project.mapLayers())
    qgs.exitQgis()

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')
True
>>> 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?

0

2 Answers 2

1

I have installed QGIS 3.4.14 in an Anaconda environment. I have put together a very similar script like yours and saved it into a file named test_script.py:

from pathlib import Path
from qgis.core import QgsApplication, QgsProject


if __name__ == "__main__":
    qgs = QgsApplication([], False)
    qgs.initQgis()

    p_prj = Path(r"d:\Projects\qgis\qgis_prj_test.qgz")
    qgs_prj = QgsProject.instance()
    qgs_prj.read(p_prj.as_posix())
    print(f"There are {qgs_prj.count()} layers in the project:")
    for name in qgs_prj.mapLayers():
        layer = qgs_prj.mapLayer(name)
        print(f"\tThe layer ID: {name} - Layer name: {layer.name()}")

    qgs.exitQgis()

When running from command line, I get the following output, which is as expected (there are indeed 2 layers in the project): enter image description here

The main commands that I use are very similar to your script so I am not sure why it would not work in your case. I trust having another script to compare with will still be useful to you.

0

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

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

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.