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?

2

1 Answer 1

0

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"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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