1

I want to create a simple standalone pyqgis script but I have some unexpected errors.

Here is my .bat file :

@echo off
SET OSGEO4W_ROOT=C:\OSGeo4W
call "%OSGEO4W_ROOT%"\bin\o4w_env.bat
call "%OSGEO4W_ROOT%"\apps\grass\grass-7.2.1\etc\env.bat
@echo off
path %PATH%;%OSGEO4W_ROOT%\apps\qgis\bin;%OSGEO4W_ROOT%\apps\grass\grass-7.2.1\lib
set QGIS_PREFIX_PATH=%OSGEO4W_ROOT:\=/%/apps/qgis
SET PYCHARM="C:\Program Files\JetBrains\PyCharm 2017.1.4\bin\pycharm.exe"

set PYTHONPATH=%PYTHONPATH%;%OSGEO4W_ROOT%\apps\qgis\python;
set PYTHONPATH=%PYTHONPATH%;%OSGEO4W_ROOT%\apps\Python27\Lib\site-packages;

set QGISPATH=%OSGEO4W_ROOT%\apps\qgis

start "PyCharm aware of QGIS" /B %PYCHARM% %*

First I try to see if the processing tool works.

Here that code :

def test1():
    qgs = QgsApplication([],True)
    QgsApplication.setPrefixPath("C:/OSGeo4W/apps/qgis", True)
    qgs.initQgis()
    sys.path.append('C:/OSGeo4W/apps/qgis/python/plugins')
    import processing
    from processing.core.Processing import Processing
    Processing.initialize()
    print processing.alglist()
    qgs.exitQgis()
test1()

Work fine I take the list correct

After I try to see if work algorithm where I want

Here the code :

def slope():
    qgs = QgsApplication([],True)
    QgsApplication.setPrefixPath("C:/OSGeo4W/apps/qgis", True)
    qgs.initQgis()
    sys.path.append('C:/OSGeo4W/apps/qgis/python/plugins')
    import processing
    from processing.core.Processing import Processing
    Processing.initialize()
    print processing.alghelp('gdalogr:slope')
    qgs.exitQgis()

slope()

and I get this message :

C:\OSGeo4W\bin\python.exe C:/Users/username/PycharmProjects/untitled/test.py
ALGORITHM: Slope
    INPUT <ParameterRaster>
    BAND <ParameterNumber>
    COMPUTE_EDGES <ParameterBoolean>
    ZEVENBERGEN <ParameterBoolean>
    AS_PERCENT <ParameterBoolean>
    SCALE <ParameterNumber>
    OUTPUT <OutputRaster>


None

Process finished with exit code 0

But anytime to try to run some script with complete code like this :

from qgis.core import QgsApplication
import sys
from qgis.core import QgsRasterLayer
from PyQt4.QtCore import QFileInfo


def slope(raster_path,slope_path):
    qgs = QgsApplication([],True)
    QgsApplication.setPrefixPath("C:/OSGeo4W/apps/qgis", True)
    qgs.initQgis()
    sys.path.append('C:/OSGeo4W/apps/qgis/python/plugins')
    import processing
    from processing.core.Processing import Processing
    Processing.initialize()
    fileInfo=QFileInfo(raster_path)
    baseName=fileInfo.baseName()
    rLayer = QgsRasterLayer(fileInfo.filePath(), baseName)
    processing.runalg('gdalogr:slope',rLayer,1.0,True,True,False,1.0,slope_path)
    qgs.exitQgis()

raster_path="C:/Users/username/Desktop/dem.tif"
slope_path="C:/Users/username/Desktop/slope.tif"
slope(raster_path,slope_path)

Then before script finishest the python.exe stops running:

image

and I get this message in pycharm :

C:\OSGeo4W\bin\python.exe C:/Users/username/PycharmProjects/untitled/test1.py

Process finished with exit code -1073741819 (0xC0000005)

I get that message for all scripts with different Python code and specific different algorithms if I want to run it standalone using pycharm editor.

Any idea why python.exe stops working?

  • 1
    The error is "Access Violation" - trying to access memory that is not valid for the process. Its possibly a library mismatch in the version of GDAL/OGR, but without a detailed understand of your specific machine configuration, its hard to say. Do you have more than one version of GDAL or libtiff installed? Could also be that your raster layer isn't loading - have you tried adding some debug after rLayer = QgsRasterLayer(fileInfo.filePath(), baseName) to make sure that its valid? – BradHards Jul 9 '17 at 7:23
  • @BradHards first my layer is valid i am sure i have test it.what you mean Access Violation?my GDAL version is 2.2.1 i dont have other version.i dont have libtiff in my python install packages in pycharm....if i remove all lines about QFileInfo and i put for input just the raster path then i take some error stop work python.exe – Mar Jul 9 '17 at 8:12
  • Access violation is what the error code of 0xC0000005 means. – BradHards Jul 9 '17 at 8:18
  • @BradHards ok and now ?how to fix it ? – Mar Jul 9 '17 at 9:33
  • If I knew, I would have posted the answer. Probably a broken install of some kind. I'm trying to help you to find out the problem. – BradHards Jul 9 '17 at 11:33

Your Answer

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

Browse other questions tagged or ask your own question.