2

I have a Python Program for doing some geoprocessing in QGIS. The starting of the Python program is given below.

import sys
from qgis.core import *
import qgis.utils
from qgis.gui import *
from PyQt4.QtGui import *
from PyQt4.QtCore import Qt
from qgis.core import QgsApplication
from PyQt4.QtGui import QApplication
QgsApplication.setPrefixPath("C:/OSGeo4W/apps/qgis", True)
QgsApplication.initQgis()
sys.path.append('C:/OSGeo4W/apps/qgis/plugins')
from processing.core.Processing import Processing
Processing.initialize()
from processing.tools import *
from PyQt4.QtCore import QFileInfo
import processing
import os
import ogr
from osgeo import ogr, gdal
import math
import xlrd

..............................many other lines.................

I want to execute this python program from a Windows Batch File, that is .bat file; even without opening QGIS software.

For that, I created a .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/Task3.py" 

Unfortunately, when I am executing this .bat file, in C:\WINDOWS\system32\cmd.exe , I am getting an error:

python.exe has stopped working 
A problem caused the program to stop working correctly. 
Windows will close the program and notify you if a solution is available.  

What modification do I have to make in .bat file or in my Python program, in order to solve this issue ?

2

I found the issue and solved it. Actually, the issue was with the line (in the python script) :

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

The 'processing' functionalities will be stored in the location C:/OSGeo4W/apps/qgis/python/plugins and not in C:/OSGeo4W/apps/qgis/plugins . But, the other core plugins will be stored in the location C:/OSGeo4W/apps/qgis/plugins .

So, for solving this issue, I have changed the line :

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

as :

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

This is the simple solution for this tricky error.

1

I tried for long and found the following points are important

  1. Always use the OSGEO4W-Shell to execute an osgeo-program, don't use cmd.exe directly. (The shell executes all batch files in %OSGEO4W_ROOT%/etc/ini where a lot of environment variables are set)
  2. To start python with full qgis support, so you can run standalone python qgis apps, use the following environment settings:

    SET QGIS_PREFIX_PATH=%OSGEO4W_ROOT:\=/%/apps/qgis SET PYTHONPATH=%OSGEO4W_ROOT%\apps\qgis\python SET PYTHONHOME=%OSGEO4W_ROOT%\apps\Python27 SET PATH=%PATH%;%OSGEO4W_ROOT%\apps\Python27\Scripts;%OSGEO4W_ROOT%\apps\qgis\bin

most of it you have already set, but mine is slightly different, as you can see. Plus the batch-files called by the osgeo4w-shell fill the %PATH% correctly.

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.