58 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 ...
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 ...
user avatar
17 votes
Accepted

Import error for qgis.core when running OSGeo4w shell script

Finally found the proper way of running processing algorithms in PyQGIS standalone scripts. This answer is based on answers to Problem with import qgis.core when writing a stand-alone PyQGIS script ...
user avatar
14 votes

Running simple Python script for QGIS from outside

I think Nathan W's answer is out of date. I was able to run QGIS (version 2.6) python scripts from the command line (Nathan's option 4) using the following commands. man qgis qgis -nologo --project /...
user avatar
  • 1,205
12 votes
Accepted

Layer is not valid error in my standalone PyQGIS script/app

To avoid "layer is not valid" errors you should carefully set your QGIS prefix path. The prefix path must be set to let QGIS know where to find resources such as data providers and the ...
user avatar
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 ...
user avatar
11 votes
Accepted

Adding layers panel (layers tree) to standalone PyQGIS application?

To complement Luigi's answer, this is how you can use the Layer Tree View for adding a legend (also known as Table of Contents or ToC) to your application. After you create your canvas in your ...
user avatar
11 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 ...
user avatar
  • 26.3k
8 votes

how to load the QGIS openlayers_plugin in standalone python script (not the QGIS embedded python console)

Being very keen on the TDD paradigm, I've spent some time putting together a dummy-interface (based on some code fragments I found on the internet) which enables calling QGIS and QGIS-plugins ...
user avatar
  • 403
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 ...
user avatar
8 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 ...
user avatar
7 votes

Import error for qgis.core when running OSGeo4w shell script

This answer is based on answers to Problem with import qgis.core when writing a stand-alone PyQGIS script and to How can I access `processing` with Python?. I suggest you to follow the work flow ...
user avatar
7 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/...
user avatar
  • 394
7 votes
Accepted

Loading raster layer in a standalone PyQGIS script

If you keep getting non valid layers, even defining them correctly, you're probably missing the QGIS prefix definition in your script (in case that you work out of QGIS). qgis_prefix="/usr" ...
user avatar
7 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 ...
user avatar
  • 6,404
6 votes

Loading raster layer in a standalone PyQGIS script

This code works in my Python Console: from qgis.core import QgsRasterLayer from PyQt4.QtCore import QFileInfo def StringToRaster(raster): # Check if string is provided fileInfo = QFileInfo(...
user avatar
  • 26.2k
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 ...
user avatar
  • 72.4k
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....
user avatar
  • 7,225
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 ...
user avatar
5 votes
Accepted

Viewer not displaying the data in my standalone PyQGIS Application

Non-valid layers are most of the times due to a wrong QGIS prefix definition. Please try with: qgis_prefix="C:\\Program Files\\QGIS Wiena\\apps\\qgis" QgsApplication.setPrefixPath(qgis_prefix, ...
user avatar
5 votes
Accepted

Deleting a standalone table from the TOC-- C# ArcObjects

Based on this Very old help you want to use your IStandaloneTableCollection IStandaloneTableCollection pColl = ArcMap.Document.FocusMap as IStandaloneTableCollection; ...
user avatar
5 votes
Accepted

QGIS Python script for creating project file

Struggled a bit for last 2 days. Abandoned XML approach. Able to accomplish first 2 tasks. Need to check how I can accomplish setting WFS,WCS parameters Create .qgs file dynamically Add Legend and ...
user avatar
  • 1,222
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
user avatar
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 ...
user avatar
  • 2,295
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....
user avatar
  • 696
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/...
user avatar
  • 3,855
5 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 ...
user avatar
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() ...
user avatar
4 votes
Accepted

Getting shapefile extent from standalone PyQGIS script?

This should be enough to get the extent of your Shapefile: layerPath = path_dir + "Input district shapefile\\" + "District.shp" extent = QgsVectorLayer( layerPath, '', 'ogr' ).extent() The result is ...
user avatar
4 votes
Accepted

"ImportError: No module named qgis.core" when importing qgis.core in Python

This error means that the qgis.core module is not in your PYTHONPATH. See the instructions in the PyQGIS cookbook on how to use the QGIS API from a python script running outside of QGIS: https://docs....
user avatar

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