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 ...
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 ...
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 ...
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 ...
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: ...
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 ...
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 ...
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/...
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 ...
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 ...
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 ...
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 ...
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....
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 ...
5
votes
Accepted
How to create a valid QgsVectorLayer from delimitedtext?
It seems you need to set the QGIS prefix path, as I explain in Loading raster layer using PyQGIS? and Failed to create memory layers in QGIS application on Linux
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 ...
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....
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/...
5
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), ...
5
votes
Accepted
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'{...
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()
...
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....
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 ...
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 ...
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)
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 ...
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....
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 ...
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.
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 ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
standalone × 167pyqgis × 118
qgis × 53
python × 29
qgis-processing × 26
qgis-3 × 23
pyqgis-3 × 12
layers × 9
qgis-plugins × 6
c++ × 6
pyqt × 6
geoserver × 5
postgis × 4
legend × 4
geowebcache × 4
qt × 4
shapefile × 3
postgresql × 3
wms × 3
print-composer × 3
installation × 3
spatial-join × 3
batch × 3
api × 3
select-by-location × 3