63 votes
Accepted

Using QGIS Processing algorithms from PyQGIS standalone scripts (outside of GUI)

You can run a QGIS Processing algorithm in standalone (no GUI) mode in this way: import sys from qgis.core import ( QgsApplication, QgsProcessingFeedback, QgsVectorLayer ) # See ...
Germán Carrillo's user avatar
19 votes
Accepted

Iterating over layers and exporting them as PNG images with PyQGIS in standalone script

In order to solve this question, we need to use timers or something that delays the execution of the script, so the canvas can reflect the layer arrangement at the time the map is exported. In other ...
Germán Carrillo's user avatar
12 votes
Accepted

Get progression bar into PyQGIS 3 standalone script

Yes - processing.run accepts a "feedback" argument, which must be an instance of a QgsProcessingFeedback subclass. If you construct your own feedback object to pass to this function, you can connect ...
ndawson's user avatar
  • 27.6k
12 votes
Accepted

Difference between iface.mapCanvas and QgsMapCanvas

On the one hand, QgsMapCanvas is a class. You can always create new canvas objects from it (as you did already in your second attempt). On the other hand, iface.mapCanvas() is an instance, an object ...
Germán Carrillo's user avatar
10 votes

Creating spatial index in standalone script as part of output vector layer using PyQGIS

If you use layer_out.dataProvider().createSpatialIndex() you don't get that error. To get the error you must use layer_out.createSpatialIndex(). The code in the question doesn't throw the error: ...
Kadir Şahbaz's user avatar
10 votes
Accepted

QGIS Stand alone python script terminating problem

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 ...
Kadir Şahbaz's user avatar
10 votes
Accepted

Hiding/showing layers in legend does not work in QGIS standalone

QgsLayerTreeMapCanvasBridge must be in global scope. But you define bridge in add_legend method's scope. Therefore, variable bridge is gone after add_legend finished. One of the solutions: Add bridge ...
Kadir Şahbaz's user avatar
8 votes
Accepted

Adding shapefile using PyQGIS

When you intend to run PyQGIS scripts out of QGIS, you need to initialize a QgsApplication so that it loads data providers and other resources. The following code snippet should work: from qgis.core ...
Germán Carrillo's user avatar
8 votes

Running simple Python script for QGIS from outside

Update for Nathan's option 4: (Windows, QGIS 2.18 Las Palmas) To print QGIS help document, qgis --help To open QGIS, load a project, then, run a python script. qgis --nologo --project c:/path/to/...
swatchai's user avatar
  • 449
8 votes
Accepted

Modifying the texture/fill of a vector layer in PyQGIS

Start with: symbol = QgsSymbolV2.defaultSymbol(layer.geometryType()) renderer = QgsSingleSymbolRendererV2(symbol) Now you have 10 types of fill: Simple fill, QgsSimpleFillSymbolLayerV2 Gradient ...
dmh126's user avatar
  • 6,732
7 votes
Accepted

PyQGIS custom application: no GUI appearing

Any GUI doesn't appear when using PyQGIS standalone application. You have to construct it. Here is an example: from qgis.core import * from qgis.gui import * from qgis.PyQt.QtWidgets import * ### GUI ...
Kadir Şahbaz's user avatar
6 votes
Accepted

Saving vector memory layer to file using PyQGIS gives empty file?

When defining a new memory layer, you need to include one of the following as the data source: Point - For a point layer. LineString - For a line layer. Polygon - For a polygon layer. So in your ...
Joseph's user avatar
  • 75.8k
6 votes
Accepted

Algorithm not found by PyQGIS?

To use native algorithms in a standalone application, you need to add the provider using: QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms()) It would be like this: from qgis....
Fran Raga's user avatar
  • 7,838
6 votes
Accepted

Making a Feature Form for a standalone PyQGIS application as in QGIS

The following script generates a simple form. You can use it in your standalone PyQGIS application to display attributes for a given feature. You can also specify fields to be displayed (or not to be ...
Kadir Şahbaz's user avatar
6 votes
Accepted

Using custom Processing algorithm from standalone PyQGIS scripts (outside of GUI)

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), ...
Germán Carrillo's user avatar
6 votes

Running the GRASS algorithm in standalone Python code using the PyQGIS processing module

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'{...
Comrade Che's user avatar
  • 7,116
5 votes

pyQGIS 3.0 script not running with processing import

I tested your code and stripped the problem down to the paths you are using. The error seems to be the forward slashes (/) that confuse your windows python installation. You should use backslashes on ...
root676's user avatar
  • 2,385
5 votes

ERROR 4: Unable to open EPSG support file gcs.csv

My GDAL_DATA environment variable was wrong, it had a line break (\n) at the end causing my error. I fixed it by forcefully changing the environment variable to remove the line break. QgsApplication....
Kezah's user avatar
  • 866
5 votes
Accepted

Adding column with area values in attributes table with standalone PyQGIS

You can execute the following code in the Python Console of QGIS. from PyQt5.QtCore import QVariant # Step 1 : Adding the Area field layer = QgsVectorLayer("C:/Users/ja/Inne/Desktop/...
Vincent Bré's user avatar
  • 4,100
5 votes

Running the GRASS algorithm in standalone Python code using the PyQGIS processing module

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 ...
Ben W's user avatar
  • 21.5k
5 votes

PyQGIS outside GUI: module 'qgis.processing' has no attribute 'run'

You also need to add the following lines: # To import section from qgis.analysis import QgsNativeAlgorithms from processing.core.Processing import Processing ... ... # after qgs.initQgis() ...
Kadir Şahbaz's user avatar
5 votes

Getting QGIS Project data from outside Python script

You have to load a project. QgsProject.instance() returns the empty project, because you didn't load any project. Add this line: project.read("c:/path/to/project.qgz") project = QgsProject....
Kadir Şahbaz's user avatar
4 votes
Accepted

Is it possible to create standalone installers for QGIS plugins?

To copy all contents of a plugin folder to QGIS plugins folder, you can use a batch script below (see code comments starting with rem). What the script does in short is to copy contents of the plugin ...
wondim's user avatar
  • 1,383
4 votes

Import error for qgis.core when running OSGeo4w shell script

I had to make minor changes to the script provided by @gcarrillo to include the OSGEO4W64 path (I had to re-install QGIS via the OSGEO4W64 installer as I used the standalone installer initially) and ...
Joseph's user avatar
  • 75.8k
4 votes
Accepted

PyQGIS Error: Cannot Create a QPixmap or Qwidget when no GUI is being used

The second parameter of the QgsApplication enables/disables the QGIS GUI components in your script. Just change it to true: qgs = QgsApplication(sys.argv, True)
Germán Carrillo's user avatar
4 votes
Accepted

if __name__=="main": in standalone Python script for QGIS

It looks like your issue might be caused by as simple syntax error. The correct syntax is if __name__ == '__main__': (with double underscores around main) That if statement is essentially used to ...
jarmokivekas's user avatar
4 votes
Accepted

from qgis._core import * ImportError: DLL load failed:

I have been working for 3 weeks for this problem. However, I found it finally. I write python console in qgis: import sys print (sys.path) It gives the path and I paste them into PYTHONPATH and PATH....
Mustafa Uçar's user avatar
4 votes
Accepted

Standalone Python application from QGIS graphical modeler

Good question. It is not obvious, but you need to copy your script algorithms to a specific folder to make your standalone script able to run them. You can easily see where your script algorithms ...
Germán Carrillo's user avatar
4 votes
Accepted

No output in Python console when using PyQGIS

In your last screenshot where we see the Python interpreter in an interactive session you have not actually started execution of the loop yet. Hit the Enter key one more time.
bugmenot123's user avatar
  • 11.1k
4 votes

Error "No module named PyQt5.QtCore" in QGIS standalone application

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 ...
Kadir Şahbaz's user avatar

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