7

Add two following lines before qp = QgsProject.instance(): qgs = QgsApplication([], False) qgs.initQgis() then, add the next line to the end of the script: qgs.exitQgis() An additional warning: You should convert ImageExportSettings() into PdfExportSettings() since you export the layout to pdf. Otherwise, you will get this error: QgsLayoutExporter....


4

Standalone PyQGIS application are still supported. I'm still developing apps. I had the same problem at the beginning of developing standalone PyQGIS3 apps. Changing all PyQt5 in import section to qgis.PyQt solved the PyQt issue. For example: from PyQt5 import QtCore, QtGui from PyQt5.QtGui import * to from qgis.PyQt import QtCore, QtGui from qgis.PyQt....


4

You must be using different Python from the installed one by QGIS installation. When I try to run the script using a different Python interpreter (C:\Program Files\Python37\python.exe) not installed by QGIS I get the same error. To be sure this is the reason: Add print("DONE!") after the line import qgis in your script above. Save the script as a ...


3

Here is how GRASS should be imported (because of a bug it does not work, see more here, thanks Andrea Giudiceandrea): import sys import qgis.core OSGeo_folder = r'C:\OSGeo4W' sys.path.append(fr'{OSGeo_folder}\apps\qgis\python\plugins') import processing from grassprovider.Grass7AlgorithmProvider import Grass7AlgorithmProvider processing.core.Processing....


3

I do not have 3.22 installed yet, so not tested in that version. I have tested this with 3.20.3 installed with the new OSGeo4W installer. If it doesn't work for you I will delete this answer. Tested workflow is: Save the following batch file (e.g. pyqgis_grass78.bat): @echo off SET OSGEO4W_ROOT=C:\OSGeo4W call "%OSGEO4W_ROOT%"\bin\o4w_env.bat call &...


2

In the end Kadir's comment pointed me to the right direction. Using project.setSelectionColor(...) did not change the color of the selection in the renderer but setting the style explicitly on the QgsMapSettings() did! So by just adding settings.setSelectionColor(QColor(255, 0, 0, 50)) to my code I got the nice transparent red I was looking for.


2

Based on Using QGIS3 Processing algorithms from standalone PyQGIS scripts (outside of GUI), you need at least 2 new files: An algorithm provider (example_algorithm_provider.py, see sample provider), which helps you register your algorithm, and Your Processing algorithm (example_processing_algorithm.py, see sample algorithm), where you define the algorithm ...


2

You can have a look at the example below. This example adds a QgsMapCanvas to a QMainWindow, loads a project, creates a QgsLayerTreeMapCanvasBridge and uses a QDockWidget to display a QgsLayerTreeView. I created a simple project to show how it works. You also need to create a QgsLayerTreeModel, passing the projects layerTreeRoot() to the constructor, and set ...


2

I find that the QgsLayerTreeMapCanvasBridge is causing some trouble for zooming on a specific layer because it updates the canvas automatically. You have to remove it from your code to make it work, here is a snippet that allows me to zoom on a specific layer, "Layer 3", in a QMainWindow: class MapViewer(QMainWindow): def __init__(self): ...


2

This is the simple PyQGIS application from the cookbook for the latest release, with all the imports that you say are failing for you included, and /usr as the QGIS prefix path which is where QgsApplication.prefixPath() says it is. This runs perfectly from a shell: from qgis.core import * from PyQt5 import QtCore, QtGui from PyQt5.QtGui import * from PyQt5....


1

Unable to reproduce with the following code (Python 3.16 on Linux) import sys from qgis.core import (QgsApplication, QgsProviderRegistry) app = QgsApplication([], True) # On Linux, didn't need to set it so commented # app.setPrefixPath("C:/Program Files/QGIS Brighton/apps/qgis", True) app.initQgis() providers = QgsProviderRegistry.instance()....


1

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 ...


1

I found the mistake. The PrefixPath for QgsApplication was wrong. The PrefixPath that is working for me under Ubuntu is the following: QgsApplication.setPrefixPath("/usr", True)


1

I believe that the proper way to deal with this problem is to add the following line after declaring an instance of QgsLayerTreeMapCanvasBridge. E.g. self.bridge = QgsLayerTreeMapCanvasBridge(self.project.layerTreeRoot(), self.canvas) self.bridge.setAutoSetupOnFirstLayer(False) You can then use the setExtent() method of the QgsMapCanvas class to set the ...


1

This answer assumes that you have an OSGeo4W64 installation of QGIS. If you have a standalone install, you will need to alter some of the files in the batch file and Python script. First, create and save a batch file with the following content. As I said, make sure all the paths are correct for your system and installation. This batch file will set up the ...


1

NNJoin There is a more advanced plug with in QGIS plugin repo called NNJoin which joins two vector layers (the input and the join layer) based on nearest neighbor relationships. please refer the page NNJoinPlugin


1

I had the same problem, and the only solution I found is to create a second script which calls the first one with the subprocess module. So in script1.py import sys qgs = QgsApplication(sys.argv, True) qgs.initQgis() pro = sysargv[1] [result] = rep.report(users[idUser][pro],idUser, pro) qgs.exec_() qgs.exitQgis() and script2.py import subprocess as sp ...


Only top voted, non community-wiki answers of a minimum length are eligible