1

I have a python script (as given below) in QGIS which will just print "Hello QGIS!".

import sys
from qgis.core import *

# Initialize QGIS Application
QgsApplication.setPrefixPath("C:/OSGeo4W/apps/qgis", True)
app = QgsApplication([], True)
QgsApplication.initQgis()

# Add the path to Processing framework
sys.path.append('C:/OSGeo4W/apps/qgis/plugins')

# Import and initialize Processing framework
from processing.core.Processing import Processing
Processing.initialize()
import processing

print 'Hello QGIS!'

Now, I want to execute this python script without opening QGIS by using a Windows Batch File script. The .bat file which I created for this is given below.

REM Change OSGEO4W_ROOT to point to the base install folder
set QGIS_PREFIX_PATH=C:\OSGeo4W\apps\qgis
REM Gdal Setup
set GDAL_DATA=C:\OSGeo4W\share\gdal\
REM Python Setup
set PATH=C:\OSGeo4W\bin;C:\OSGeo4W\apps\qgis\bin;%PATH%
SET PYTHONHOME=C:\OSGeo4W\apps\Python27
set PYTHONPATH=C:\OSGeo4W\apps\qgis\python;%PYTHONPATH%

REM Launch python job
python C:/Users/Sreeraj/.qgis2/processing/scripts/hello.py
pause

Unfortunately, I am getting an error :

File "C:/Users/Sreeraj/.qgis2/processing/scripts/hello.py", line 13, in <module>
from processing.core.Processing import Processing
ImportError: No module named processing.core.Processing

I also tried modifying the .bat file as given below.

SET QGIS_PREFIX_PATH=C:\OSGeo4W\apps\qgis
call "C:/OSGeo4W/bin/o4w_env.bat"
SET PYTHONPATH=C:\OSGeo4W\apps\qgis\python;%PYTHONPATH%
SET PATH=%PATH%;C:\OSGeo4W\apps\qgis\bin

cmd /c python "C:/Users/Sreeraj/.qgis2/processing/scripts/hello.py" 

Again, I am getting the same error :

ImportError: No module named processing.core.Processing

But, clearly, I have set everything correctly and the processing folder is located in C:/Users/Sreeraj/.qgis2/ .

So, what kind of modification do I have to make in order to run this basic python script correctly using a .bat file ?

  • Try replacing import processing with from processing.tools import *. – Joseph Feb 12 '18 at 11:44
  • 1
    @Joseph I tried that and it didn't work. – Sreeraj Feb 12 '18 at 11:46
  • 1
    @Joseph Is there some other folder called 'processing' in any other location ? Currently, I can see a folder named 'processing' in the location C:/Users/Sreeraj/.qgis2/ . And my python script hello.py is saved in the location "C:/Users/Sreeraj/.qgis2/processing/scripts/hello.py" – Sreeraj Feb 12 '18 at 11:53
  • 1
    @Joseph This processing folder "C:/Users/Sreeraj/.qgis2/processing/" is not in the QGIS_PREFIX_PATH=C:\OSGeo4W\apps\qgis . Will that make a problem ? – Sreeraj Feb 12 '18 at 11:59
  • 2
    @Joseph You are right !! I have to give sys.path.append("C:/OSGeo4W/apps/qgis/python/plugins") and not sys.path.append("C:/OSGeo4W/apps/qgis/plugins") . It worked perfectly. It was a very silly and simple mistake from my side. Thank You. – Sreeraj Feb 12 '18 at 12:15
1

You need to replace the following line:

sys.path.append('C:/OSGeo4W/apps/qgis/plugins')

with:

sys.path.append('C:/OSGeo4W/apps/qgis/python/plugins')

As this is where the Processing functionalities are stored (the path you initially used contains other core plugins)

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.