2

I am trying to get started with pyQGIS but failing pretty hard.
Trying to follow http://docs.qgis.org/testing/en/docs/pyqgis_developer_cookbook/intro.html
A lot of it seems incomplete for Windows users, especially if they happen to have another python install...

OS: Windows10
QGIS: 2.10.1
PATH:

    C:\Python27\;
    C:\Python27\Scripts;
    C:\Program Files\QGIS Pisa\bin;
    C:\Program Files\QGIS Pisa\apps\qgis\bin;

PYTHONPATH:

    C:\Program Files\QGIS Pisa\apps\qgis\python;
    C:\Program Files\QGIS Pisa\apps\qgis\python\plugins;
    C:\Program Files\QGIS Pisa\apps\Python27\lib\

test.py:

    from qgis.core import *

    # supply path to where is your qgis installed
    QgsApplication.setPrefixPath("C:/program files/qgis pisa/apps/qgis", True)

    # load providers
    QgsApplication.initQgis()

    data_source = "..." #many different files.
    layer_name = "nctasdfasd"
    provider_name = "ogr"

    layer = QgsVectorLayer(data_source, layer_name, provider_name)

    if not layer.isValid():
      print "Layer failed to load!"

    QgsApplication.exitQgis()

cmdline: /cygdrive/c/Program\ Files/QGIS\ Pisa/bin/python.exe test.py
(Have tried from cygwin and win cmd - no diff)

output:
If i leave the shapefile zipped, get some warnings about recoding CP_OEMCP to UTF-8. If i unzip and point at .shp file, no output. If i run in cygwin, get "Segmentation fault", If i run in cmd, get windows crash dialog.

test shapefiles:
http://gis.nct.opendata.arcgis.com/datasets/438b8aeacb42463f9e3f1ff308ee507b_2
random census data from census.gov cb_2014_us_cd114_500k

Is there any way to get more information about the crash/failure? Getting to this point took a long time fighting silent errors about the python env and qgis prefixpath. I suspect there may still be an issue there, but don't know how to test/debug.

Edit: this is unlikely to help, but I upgraded to Essen and attached VS2015 to get a call stack. Is there a debug release available so I could get symbol names?

QtCore4.dll!00000000720d1e10()  Unknown
qca.dll!00007ff84af58af7()  Unknown
qca.dll!00007ff84af5dec7()  Unknown
qca.dll!00007ff84af5e5c3()  Unknown
qca.dll!00007ff84af41d74()  Unknown
qgis_core.dll!00007ff840a9305f()    Unknown
qgis_core.dll!00007ff840a9841f()    Unknown
_core.pyd!00007ff84160dba9()    Unknown
python27.dll!000000001e0c20a9() Unknown
python27.dll!000000001e112524() Unknown
python27.dll!000000001e115cd4() Unknown
python27.dll!000000001e1174d9() Unknown
python27.dll!000000001e117579() Unknown
python27.dll!000000001e143a9a() Unknown
python27.dll!000000001e144d8a() Unknown
python27.dll!000000001e1453e0() Unknown
python27.dll!000000001e145e23() Unknown
python27.dll!000000001e044c25() Unknown
python.exe!000000001d00119e()   Unknown

More edits: By adding this statement before setPrefix:

app=QgsApplication( [], False)

I got another error message about the GDAL_DATA env which I set to

C:\Program Files\QGIS Essen\share\epsg_csv

However, I still cannot load a layer. Still just crashing.

4
  • If you start with PyQGIS, why don't you begin with the Python console of QGIS (and not with a script from outside) ?
    – gene
    Commented Mar 6, 2016 at 19:45
  • Running the QgsVectorLayer cmd from the QGIS console works (or at least doesn't crash). But I intend to be writing complicated enough scripts that I want to be able to save/edit them in a separate file, and use cmdline tools to view/process output... Commented Mar 6, 2016 at 20:06
  • One thing at a time, first start PyQGIS in the console or as a processing script.
    – gene
    Commented Mar 6, 2016 at 20:30
  • Please be patient w/ me, but isn't PyQGIS just the name for QGIS's python binding? I'm not sure what you're asking me. Commented Mar 6, 2016 at 22:11

1 Answer 1

1

OK, I fixed my issues. the Bold items were the stuff I was missing.

  1. Make sure PATH and PYTHONPATH are set as above. It may be useful to create a new env var 'QGIS'

    C:\Program Files\QGIS Essen
    
  2. Create an instance of QgsApplication

    app=QgsApplication( [], False)
    

    use this instance to setPrefix, init, and close:

    app.setPrefixPath("C:/program files/qgis essen/apps/qgis", True)
    app.initQgis()
    
    #Other stuff
    
    app.exitQgis()
    
  3. Make sure GDAL_DATA is set to epsg_csv folder:

     C:\Program Files\QGIS Essen\share\epsg_csv
    

Entire working script is here:

import os
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.core import *


app=QgsApplication( [], False)

# supply path to where is your qgis installed
app.setPrefixPath( os.environ["QGIS"] + "/apps/qgis", True)

# load providers
app.initQgis()

data_source = "" #set path to .shp file!
layer_name = "nct"
provider_name = "ogr"

layer = QgsVectorLayer(data_source, layer_name, provider_name)
if not layer.isValid():
    print "Layer failed to load!"
else:
    print "valid!"
app.exitQgis()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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