3

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()

1 Answer 1

3

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_()
1
  • 1
    This works, thank you! I realized now that the script I have written is not really what I'm trying to accomplish. I want a standalone script that controls QGIS similarly to the built-in Python Console, if that's even possible. Jul 29, 2021 at 16:09

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.