1

I am trying to run a standalone script to run PyQGIS 3.22 processes faster.

Call PyQgis.bat via cmd:

@echo off

SET OSGEO4W_ROOT=C:\OSGeo4W SET QGISNAME=qgis-ltr SET QGIS=%OSGEO4W_ROOT%\apps%QGISNAME% set QGIS_PREFIX_PATH=%QGIS%

CALL "%OSGEO4W_ROOT%\bin\o4w_env.bat"

REM Python Setup set PATH=%OSGEO4W_ROOT%\bin;%QGIS%\bin;%PATH% SET PYTHONHOME=%OSGEO4W_ROOT%\apps\Python39 set PYTHONPATH=%QGIS%\python;%PYTHONPATH%

ECHO OSGeo path is: %OSGEO4W_ROOT% ECHO Getting QGIS libs from: %QGIS% ECHO Python loaded from: %PYTHONHOME%

C:\OSGeo4W\bin\python-qgis-ltr.bat C:\Users\Shadow\Desktop\PyQgis\Intersection.py %*

Intersection.py looks like this:

from qgis.core import *
import qgis.utils
import glob

i=0 print("start") gemeinden = r"C:\Users\Shadow\Desktop\PyQgis\Gemeinden" potenzial = r"C:\Users\Shadow\Desktop\PyQgis\Shape-files" shp_files = glob.glob(gemeinden+"\*.shp") for file in shp_files: i += 1 #if i <= 53: # pass #else: print(str(i)+","+str(len(shp_files))+"()"+file[:-4][41:]) output = r"C:\Users\Shadow\Desktop\PyQgis\Gemeinden\"+file[:-4][41:]+".shp" ergebnis = processing.run('saga:intersect', {'A':file, 'B':potenzial, 'RESULT':output})

The Error:

NameError: name 'processing' is not defined

Everything works fine when I start the script inside the QGIS Python-console.

Do I have to import some modules inside the Intersection.py or are some environments missing in the PyQgis.bat?

Vince
  • 20,017
  • 15
  • 45
  • 64
Mabignix
  • 88
  • 9
  • Have a look at: https://gis.stackexchange.com/questions/279874/using-qgis-processing-algorithms-from-pyqgis-standalone-scripts-outside-of-gui and https://gis.stackexchange.com/questions/286281/why-does-qgis-3-2-nativeextractvertices-algorithm-not-work-properly-in-standa – Ben W Sep 23 '22 at 11:16
  • It doesn't seem to be explained in those links but I think its because plugins can be in a system or user profile location, and QGIS Desktop will add those locations to the import path for that user profile. A PyQGIS script doesn't have a user profile so you have to set it explicitly. – Spacedman Sep 23 '22 at 11:30

1 Answers1

1

You need a few modifications to your Intersection.py file to import and initialize processing and also to initialize the qgis application for running a standalone script.

This is based on answers to the questions I linked to in my comment- I have just adapted and tested here to make sure things are still working.

I made a test with the following files (with just the paths to my files changed) in QGIS 3.22.11. Double clicking the batch file ran the python script successfully and produced the output shapefile.

Intersection.py:

import sys

from qgis.core import QgsApplication

QgsApplication.setPrefixPath('C:/OSGeo4W/apps/qgis-ltr', True) qgs = QgsApplication([], False) qgs.initQgis()

sys.path.append('C:\OSGeo4W\apps\qgis-ltr\python\plugins')

import processing from processing.core.Processing import Processing Processing.initialize()

input_a = 'C:\Users\Path\To\Input_file_1.shp' input_b = 'C:\Users\Path\To\Input_file_2.shp' output = 'C:\Users\Path\To\Output_file.shp'

alg_params = {'A':input_a, 'B':input_b, 'RESULT':output, 'SPLIT':True}

processing.run("saga:intersect", alg_params)

My batch file looks like this:

@echo off
SET OSGEO4W_ROOT=C:\OSGeo4W
call "%OSGEO4W_ROOT%"\bin\o4w_env.bat
@echo off
path %PATH%;%OSGEO4W_ROOT%\apps\qgis-ltr\bin
path %PATH%;C:\OSGeo4W\apps\Qt5\bin
path %PATH%;C:\OSGeo4W\apps\Python39\Scripts
set QGIS_PREFIX_PATH=%OSGEO4W_ROOT:\=/%/apps/qgis-ltr
set GDAL_FILENAME_IS_UTF8=YES
rem Set VSI cache to be used as buffer, see #6448
set VSI_CACHE=TRUE
set VSI_CACHE_SIZE=1000000
set PYTHONPATH=%PYTHONPATH%;%OSGEO4W_ROOT%\apps\qgis-ltr\python
set PYTHONHOME=%OSGEO4W_ROOT%\apps\Python39
set QT_PLUGIN_PATH=%OSGEO4W_ROOT%\apps\qgis-ltr\qtplugins;%OSGEO4W_ROOT%\apps\qt5\plugins

python "C:\Users\Path\To\Intersection.py"

Ben W
  • 21,426
  • 3
  • 15
  • 39
  • 1
    Thanks a lot, works like a charm! – Mabignix Sep 28 '22 at 10:29
  • @Mabignix, you're welcome :-) – Ben W Sep 28 '22 at 11:00
  • do you happen to know how to add grass functions to pyqgis? – Mabignix Sep 28 '22 at 11:43
  • @Mabignix, this changed from 3.22 on. I have not tried recently but you can see discussion in link below. The accepted answer (not mine) might work now: https://gis.stackexchange.com/questions/420721/running-the-grass-algorithm-in-standalone-python-code-using-the-pyqgis-processin/420734 – Ben W Sep 28 '22 at 13:46