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:


Setting the following environment vars:


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.

  • 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, 2022 at 22:25
  • Thanks, but unfortunately I got the same error :(
    – Mar Tijn
    Jan 17, 2022 at 23:07
  • Look at the accepted answer here: gis.stackexchange.com/questions/279874/…
    – Ben W
    Jan 17, 2022 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 <module> from qgis._gui import * File "processing\tools\dataobjects.py", line 38, in <module> from qgis.gui import QgsSublayersDialog ...<module> from processing.tools.dataobjects import * # NOQA File "C:\svn\qgis\main.py", line 20, in <module> import processing
    – Mar Tijn
    Jan 17, 2022 at 23:36

2 Answers 2


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


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
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 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.layer_treeview = QgsLayerTreeView()

        self.layer_tree_dock = QDockWidget("Layers")

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

        self.layout = QHBoxLayout()
        self.contents = QWidget()


    def load_layers(self):
        source_path = "path/to/data/source"
        layer = QgsVectorLayer(source_path, 'Layer1', 'ogr')

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


main_window = MapViewer()


enter image description here

  • 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 <module> from processing.core.Processing import Processing
    – Mar Tijn
    Jan 17, 2022 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 <module> from qgis._gui import * File "C:\svn\qgis\main.py", line 7, in <module> from qgis.gui import *
    – Mar Tijn
    Jan 17, 2022 at 23:05
  • How do you open IDE? Do you use VS Code? Jan 17, 2022 at 23:28
  • I test this code in VS Code indeed.
    – Mar Tijn
    Jan 17, 2022 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. Jan 17, 2022 at 23:35

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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.