Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm using PyScripter to explore PyQGIS API because of it's very handy auto-completion function.

I've managed to get a handle on a PostGis vector layer by:

>>> from qgis.core import *
>>> uri = QgsDataSourceURI()
>>> uri.setConnection("<some URL>","5432","<DB name>","<user>","<password>")
>>> uri.setDataSource("public","<table name>","<geometry column name>")
>>> vLayer=QgsVectorLayer(uri.uri(),"<layer name>","postgres")
>>> vLayer
<qgis.core.QgsVectorLayer object at 0x052B4D68>

So far so good. My next step is to explore the layer's dataProvider.

>>> vLayer.dataProvider()

Why doesn't vLayer.dataProvider() yield anything, like in QGIS Python Console?

Do I need to initialize vLayer more?

share|improve this question
up vote 2 down vote accepted

You haven't setup your QGIS Python environment correctly.

At the start of your script you need to put these four lines in order to tell the QGIS libs where to look for providers.

qgishome = 'C:\OSGeo4W\apps\qgis-dev\'
app = QgsApplication([], True)
QgsApplication.setPrefixPath(qgishome, True)

when you need to exit the QGIS session you should use:


I have a growing simple set of examples at which can help to understand the basics of getting something setup. See the canvas example

share|improve this answer
Thanks. That works! – spingerfitz Jan 10 '13 at 13:45
Note that I had to add the qgis-devel library first with the OSGeo4W-installer and my qgishomepath = 'c:/OSGeo4W/apps/qgis'. I've had setup QGIS with the Express Desktop Install.. – spingerfitz Jan 10 '13 at 13:53
don't forget to tick the accept button if this is the right – Nathan W Jan 10 '13 at 14:04

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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