Trying to get started with pyQgis and failing hard. My end goal is to take geometry attributes from a .shp file and add it to a .csv file in order to spatially represent details contained in the .csv.

To elaborate: I have a .csv of NYC permits pulled and a .shp of tax maps. I've created matching fields by manipulating Borough Block and Lot data. The trick is, joining is a many to one problem. There is only one tax lot and an unknown number of permits for that tax lot. I see easiest route as adding geometry fields to each permit entry.

I'm using the cookbook and have looked extensively through the questions here but the answers which seem to solve my problems have not.

Below is my code:

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

qgis=QgsApplication([], False)

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

#supply path to qgis install location--same as above
#QgsApplication.setPrefixPath("C:\PROGRA~1\QGIS2~1.16\apps\qgis", True)

#load providers

permits_csv=QgsVectorLayer(permits_uri, 'permits', 'ogr')

print QgsApplication.showSettings()

print (QgsProviderRegistry.instance().providerList())

if not taxlots_shp.isValid():
  print "Layer1 failed to load!"

if not permits_csv.isValid():
  print "Layer2 failed to load!"

I have been able to run other very similar versions without error, but continues to print 'failed to load'. This version of the code incorporates a few lines which may be wrong. I was trying a few methods to troubleshoot.

I should add that my latest hurdle is that after running the code once qgis stops responding and I must close it and reopen to try again. Not sure what that's about.

From what I've read my problem must be in the paths I'm setting...so below are the paths.

Verbatim according to the properties of the qgis shortcut: "C:\Program Files\QGIS 2.18\bin\nircmd.exe" exec hide C:\PROGRA~1\QGIS2~1.18\bin\qgis.bat

When I printed the prefix path through the python cmd line I got: C:/PROGRA~1/QGIS2~1.16/apps/qgis

Any clarification would be great! Obviously I haven't even gotten to my iteration loops as I can't even get past the first step.


There a typo in your imports, please use:

from qgis.core import *

Then, what happens if you escape your path? I mean, using:




instead of what you provided?

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.