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.5k16 gold badges49 silver badges65 bronze badges
asked Sep 23, 2022 at 10:15
2
  • Have a look at: gis.stackexchange.com/questions/279874/… and gis.stackexchange.com/questions/286281/… Commented Sep 23, 2022 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. Commented Sep 23, 2022 at 11:30

1 Answer 1

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"
answered Sep 24, 2022 at 5:09
4
  • 1
    Thanks a lot, works like a charm! Commented Sep 28, 2022 at 10:29
  • @Mabignix, you're welcome :-) Commented Sep 28, 2022 at 11:00
  • do you happen to know how to add grass functions to pyqgis? Commented Sep 28, 2022 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: gis.stackexchange.com/questions/420721/… Commented Sep 28, 2022 at 13:46

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.