0

I'm trying to build a launch.bat for qgis 3 python. I would want to run python via batch file but I get an error 'no module named processing'

Launch.bat:

@ECHO OFF 

set OSGEO4W_ROOT=C:\OSGeo4W64

@echo off
call "%OSGEO4W_ROOT%\bin\o4w_env.bat"
call "%OSGEO4W_ROOT%\bin\qt5_env.bat"
call "%OSGEO4W_ROOT%\bin\py3_env.bat"

@echo off
path %OSGEO4W_ROOT%\apps\qgis\bin;%PATH%
set QGIS_PREFIX_PATH=%OSGEO4W_ROOT:\=/%/apps/qgis
set GDAL_FILENAME_IS_UTF8=YES

set VSI_CACHE=TRUE
set VSI_CACHE_SIZE=1000000
set QT_PLUGIN_PATH=%OSGEO4W_ROOT%\apps\qgis\qtplugins;%OSGEO4W_ROOT%\apps\qt5\plugins
set PYTHONPATH=%OSGEO4W_ROOT%\apps\qgis\python;%PYTHONPATH%

python D:\Testi2\testi.py

pause

Is the problem within those CALL batch files?


I think the problem might be in the python file I'm trying to run:

    from qgis.PyQt.QtCore import QCoreApplication
from qgis.core import (QgsProcessing,
                       QgsProcessingAlgorithm,
                       QgsProcessingException,
                       QgsProcessingOutputNumber,
                       QgsProcessingParameterDistance,
                       QgsProcessingParameterFeatureSource,
                       QgsProcessingParameterVectorDestination,
                       QgsProcessingParameterRasterDestination)
import processing

etc.

I also tried adding..

# Prepare the environment
import sys
from qgis.core import QgsApplication
from PyQt5.QtGui import QApplication
app = QApplication([])
QgsApplication.setPrefixPath('C:\OSGeo4W64\bin', True)
QgsApplication.initQgis()

# Prepare processing framework 
sys.path.append('C:\OSGeo4W64\apps\qgis\python\plugins')
from processing.core.Processing import Processing
Processing.initialize()

from qgis.PyQt.QtCore import QCoreApplication
from qgis.core import (QgsProcessing,
                       QgsProcessingAlgorithm,
                       QgsProcessingException,
                       QgsProcessingOutputNumber,
                       QgsProcessingParameterDistance,
                       QgsProcessingParameterFeatureSource,
                       QgsProcessingParameterVectorDestination,
                       QgsProcessingParameterRasterDestination)
import processing

But it doesn't start qapplication via Pygt5.gtgui

1

You also need to set the python path to processing:

set PYTHONPATH=%OSGEO4W_ROOT%\apps\qgis\python;%PYTHONPATH%
set PYTHONPATH=%OSGEO4W_ROOT%\apps\qgis\python\plugins\processing;%PYTHONPATH%
2
  • now I get an error ' no module named qgis'. Any suggestions? – woundari Sep 20 '19 at 22:28
  • I don't use QGIS on Windows (I use Linux), but in QGIS, click Plugins menu->Python Console and run import os,sys;print(sys.path);print(os.path.dirname(os.path.dirname(qgis.__file__))) to figure out the paths you need to add to PYTHONPATH in your batch file. – user2856 Sep 20 '19 at 22:45

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.