Loading QGIS project file with standalone PyQGIS Script

pyqgisqgis-3standalone

I've been working on a standalone PyQGIS script that loads a QGIS project file and opens it on the main canvas. I've written the script directly based on the PyQGIS Developer Cookbook here: https://docs.qgis.org/3.16/en/docs/pyqgis_developer_cookbook/loadproject.html

Here is my script:

from qgis.core import * 
from qgis.core import (
    QgsProject,
    QgsPathResolver
)

from qgis.utils import iface

from qgis.gui import (
    QgsLayerTreeMapCanvasBridge, QgsMapCanvas
)

import sys, os

sys.path.append("C:/OSGeo4W/apps/qgis-ltr/python/plugins")

# Supply path to qgis install location
QgsApplication.setPrefixPath("C:/OSGeo4W/apps/qgis-ltr", True)

# Create a reference to the QgsApplication.  Setting the
# second argument to False disables the GUI.
qgs = QgsApplication([], True)
qgs.initQgis()

print("\nQGIS initialized")

#Get the project instance
project = QgsProject.instance()

print(qgs.showSettings())

canvas = QgsMapCanvas()

bridge = QgsLayerTreeMapCanvasBridge( \
         project.layerTreeRoot(), canvas)

#Load another project
project.read("N:/companyfiles/08-Web Map Services/0_Built Products/3-ENPLAN internal projects/Jobs Active AOIs/testproject.qgz")

# print("Opened " + os.path.basename(project.fileName()))

# Finally, exitQgis() is called to remove the
# provider and layer registries from memory
qgs.exitQgis()

The issue is that the code runs with one error, and then nothing happens. The error I keep receiving is the following:

qt.network.monitor: Could not get connectivity: "The specified service does not exist as an installed service."

The read() line of code runs fine in the QGIS Python Console. What am I missing?

I have a cmd script that sets up the various paths before running any Python script via the command prompt:

@echo off
SET OSGEO4W_ROOT=C:\OSGeo4W
call "%OSGEO4W_ROOT%"\bin\o4w_env.bat
call "%OSGEO4W_ROOT%"\apps\grass\grass78\etc\env.bat

@echo off
path %PATH%;%OSGEO4W_ROOT%\apps\qgis-ltr\bin
path %PATH%;%OSGEO4W_ROOT%\apps\grass\grass78\lib
path %PATH%;C:\OSGeo4W\apps\Qt5\bin
path %PATH%;C:\OSGeo4W\apps\Python39\Scripts
path %PATH%;C:\OSGeo4W\apps\Qt5\plugins

set PYTHONPATH=%PYTHONPATH%;%OSGEO4W_ROOT%\apps\qgis-ltr\python
set PYTHONHOME=%OSGEO4W_ROOT%\apps\Python39

set QT_PLUGIN_PATH=%OSGEO4W_ROOT%\apps\qgis-ltr\qtplugins;%OSGEO4W_ROOT%\apps\Qt5\plugins
set QT_QPA_PLATFORM_PLUGIN_PATH=%OSGEO4W_ROOT%\apps\Qt5\plugins\platforms
set QGIS_PREFIX_PATH=%OSGEO4W_ROOT%\apps\qgis-ltr

set PATH=C:\OSGeo4W\apps\qgis-ltr\bin;C:\OSGeo4W\apps\qgis-ltr\bin;%PATH%

cmd.exe

It seems to work because this code works fine with the same error:

import sys

from qgis.core import * 
from qgis.core import (
    QgsProject,
    QgsPathResolver
)

sys.path.append(r"C:/OSGeo4W/apps/qgis-ltr/python/plugins")

QgsApplication.setPrefixPath("C:/OSGeo4W/apps/qgis-ltr", True)

qgs = QgsApplication([], False)
qgs.initQgis()

import processing
from processing.core.Processing import Processing
Processing.initialize()

for alg in QgsApplication.processingRegistry().algorithms():
    print("{}:{} --> {}".format(alg.provider().name(), alg.name(), alg.displayName()))

qgs.exitQgis()

Best Answer

You also need to create a running QApplication instance and call show() on the canvas object.

I think the snippet below is about the minimum amount of code you will need to read a project and show it on a canvas in a standalone pyqgis/pyqt application:

from PyQt5.QtWidgets import QApplication
from qgis.core import QgsApplication, QgsProject
from qgis.gui import QgsMapCanvas, QgsLayerTreeMapCanvasBridge


app = QApplication([])
qgs = QgsApplication([], False)
qgs.setPrefixPath("C:/OSGeo4W64/apps/qgis-ltr", True)
qgs.initQgis()

canvas = QgsMapCanvas()

project = QgsProject.instance()
bridge = QgsLayerTreeMapCanvasBridge(project.layerTreeRoot(), canvas)
project.read("N:/companyfiles/08-Web Map Services/0_Built Products/3-ENPLAN internal projects/Jobs Active AOIs/testproject.qgz")

canvas.show()

app.exec_()