4

When trying to use processing.run() from a standalone (no GUI) mode, I get the following error:

module 'qgis.processing' has no attribute 'run'

in the following trivial example:


from qgis import processing
result = processing.run("native:buffer", {'INPUT': 'test', 'OUTPUT': 'memory:'})

Other methods, like algorithmHelp(), don't seem to work either.

I followed the Windows setup instructions from here:

https://docs.qgis.org/3.16/en/docs/pyqgis_developer_cookbook/intro.html#running-custom-applications

Setting the following environment vars:

PYTHONPATH=C:\OSGeo4W\apps\qgis\python

and PATH is appended with C:\OSGeo4W\bin;C:\OSGeo4W\apps\qgis\bin

Anything I'm missing here?

NOTE: When running the same script from inside the GUI (using the Script editor), all goes well and I don't get these errors. Somehow the environment seems to be different between the two.

QGIS version is 3.22.2-Białowieża.

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
Mar Tijn
  • 503
  • 3
  • 10
  • I'm really not an expert on this, but I was recently experiencing a similar error when using a script in the layer action context, that I had been working with for some time in the script editor where I didn't get that kind of error. What worked for me was to ensure the 'from qgis import processing' was the very first line of the script. But the if your trivial example still comes up with the same error, I guess that that solution is no help in your case unfortunately. – laqzww Jan 17 '22 at 22:25
  • Thanks, but unfortunately I got the same error :( – Mar Tijn Jan 17 '22 at 23:07
  • Look at the accepted answer here: https://gis.stackexchange.com/questions/279874/using-qgis-3-processing-algorithms-from-pyqgis-standalone-scripts-outside-of-gu – Ben W Jan 17 '22 at 23:11
  • @BenW thanks! I actually tried that earlier today, but got this weird error below. And since that answer was posted 3 years ago, I figured this setup probably changed. Any thoughts? qgis._gui cannot import type '����' from PyQt5.QtCore File "C:\OSGeo4W\apps\qgis\python\qgis\gui__init__.py", line 25, in from qgis._gui import * File "processing\tools\dataobjects.py", line 38, in from qgis.gui import QgsSublayersDialog ... from processing.tools.dataobjects import * # NOQA File "C:\svn\qgis\main.py", line 20, in import processing – Mar Tijn Jan 17 '22 at 23:36

2 Answers2

5

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

Processing.initialize() QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms())

... ...

Sometimes developing a PyQGIS standalone application can be tiring. Especially for those new to this topic. You need to open your editor using a batch file like this:

@echo off
SET OSGEO4W_ROOT="C:\OSGeo4W"
call %OSGEO4W_ROOT%\bin\o4w_env.bat

path %PATH%;%OSGEO4W_ROOT%\apps\qgis\bin path %PATH%;%OSGEO4W_ROOT%\apps\Qt5\bin path %PATH%;%OSGEO4W_ROOT%\apps\Python39\Scripts set QGIS_PREFIX=%OSGEO4W_ROOT%\apps\qgis set PYTHONHOME=%OSGEO4W_ROOT%\apps\Python39 set PYTHONPATH=%OSGEO4W_ROOT%\apps\qgis\python;%OSGEO4W_ROOT%\apps\qgis\python\plugins;%PYTHONPATH% set QT_QPA_PLATFORM_PLUGIN_PATH=%OSGEO4W_ROOT%\apps\Qt5\plugins\platforms set QT_PLUGIN_PATH=%OSGEO4W_ROOT%\apps\qgis\qtplugins;%OSGEO4W_ROOT\apps\qt5\plugins

set GDAL_FILENAME_IS_UTF8=YES set VSI_CACHE=TRUE set VSI_CACHE_SIZE=1000000

start "Visual Studio QGIS" /B "C:\Program Files\Microsoft VS Code\Code.exe" %*


A working example here: (change source_path).

from qgis.core import *
from qgis.gui import *
from qgis.PyQt.QtWidgets import *

import processing from qgis.analysis import QgsNativeAlgorithms from processing.core.Processing import Processing

GUI Construction

class MapViewer(QMainWindow): def init(self): QMainWindow.init(self, None)

    self._canvas = QgsMapCanvas()
    self._root = QgsProject.instance().layerTreeRoot()

    self.bridge = QgsLayerTreeMapCanvasBridge(self._root, self._canvas)
    self.model = QgsLayerTreeModel(self._root)
    self.model.setFlag(0x25043)
    self.model.setFlag(QgsLayerTreeModel.ShowLegend)
    self.layer_treeview = QgsLayerTreeView()
    self.layer_treeview.setModel(self.model)

    self.layer_tree_dock = QDockWidget("Layers")
    self.layer_tree_dock.setObjectName("layers")
    self.layer_tree_dock.setFeatures(QDockWidget.NoDockWidgetFeatures)
    self.layer_tree_dock.setWidget(self.layer_treeview)

    self.splitter = QSplitter()
    self.splitter.addWidget(self.layer_tree_dock)
    self.splitter.addWidget(self._canvas)
    self.splitter.setCollapsible(0, False)
    self.splitter.setStretchFactor(1, 1)

    self.layout = QHBoxLayout()
    self.layout.addWidget(self.splitter)
    self.contents = QWidget()
    self.contents.setLayout(self.layout)
    self.setCentralWidget(self.contents)

    self.load_layers()

def load_layers(self):
    source_path = "path/to/data/source"
    layer = QgsVectorLayer(source_path, 'Layer1', 'ogr')
    QgsProject.instance().addMapLayer(layer)
    self._canvas.setExtent(layer.extent())
    self._canvas.setLayers([layer])

    ### PROCESSING
    result = processing.run("native:buffer", {'INPUT': source_path, 'DISTANCE':10, 'OUTPUT': 'memory:'})
    QgsProject.instance().addMapLayer(result["OUTPUT"])

########################

qgs = QgsApplication([], True) qgs.initQgis()

Processing.initialize() QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms())

main_window = MapViewer() main_window.show()

qgs.exec_() qgs.exitQgis()

enter image description here

Kadir Şahbaz
  • 76,800
  • 56
  • 247
  • 389
  • 1
    Thanks! Where did you get this information from? It's not in the official docs about writing external scripts, as that simply mentioned from qgis import processing and that's it. Your example unfortunately gave me the following error: Exception has occurred: ModuleNotFoundError No module named 'processing' File "C:\svn\qgis\main.py", line 8, in from processing.core.Processing import Processing – Mar Tijn Jan 17 '22 at 22:58
  • 1
    Haha yes it definitely is missing. It's mostly trial and error and looking up articles here, than that I could use the official documentation. When I try to run your example as is, I get a very weird error: Exception has occurred: RuntimeError qgis._gui cannot import type '����' from PyQt5.QtCore File "C:\OSGeo4W\apps\qgis\python\qgis\gui__init__.py", line 25, in from qgis._gui import * File "C:\svn\qgis\main.py", line 7, in from qgis.gui import * – Mar Tijn Jan 17 '22 at 23:05
  • How do you open IDE? Do you use VS Code? – Kadir Şahbaz Jan 17 '22 at 23:28
  • I test this code in VS Code indeed. – Mar Tijn Jan 17 '22 at 23:31
  • I use VS Code, too. I open it using a batch file, which contains settings for some environment variables. And the example runs seamlessly. – Kadir Şahbaz Jan 17 '22 at 23:35
  • Would it be possible for you to share (parts of) that .bat file with me? – Mar Tijn Jan 17 '22 at 23:37
  • I have added the related batch file content I use. – Kadir Şahbaz Jan 17 '22 at 23:47
  • Thanks! Unfortunately this still gives me the: qgis._gui cannot import type '����' from PyQt5.QtCore error. I'll try a reinstall :( – Mar Tijn Jan 18 '22 at 08:13
  • Turns out to be a conflicting PyQT install. So I removed that. But then I got into the problem that PyQt couldn't be found. So I took the batch file from here: https://gis.stackexchange.com/questions/337558/import-qgis-core-in-pycharm-no-module-named-pyqt5-qtcore/337623#337623 which got me a bit further, now processing errored with No module named 'osgeo'. It's really incredible how frustratingly difficult this is, especially with no official docs on it – Mar Tijn Jan 18 '22 at 08:43
  • I found a solution that works and created a GitHub repo for it https://github.com/MarByteBeep/pyqgis-standalone perhaps you may find it useful. Thanks a lot for all the answers! – Mar Tijn Jan 18 '22 at 20:42
0

This answer is posted here:

Looking for manual on how to properly setup standalone PyQGIS without GUI

At the time of writing I didn't know the two were related. But I found a workable solution in that post. Thanks all for your answers here!

Mar Tijn
  • 503
  • 3
  • 10