Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I am having trouble creating a QGIS vector layer in a stand-alone python script while exactly the same code works fine in python console inside QGIS. The following code results in a proper displayable layer when executed in the python console:

layer = QgsVectorLayer('/abs/path/buses.shp', 'buses', 'ogr')

However, the same line of code results in an invalid layer in a standalone app (other QGIS functionality seems to work fine):

# PYTHONPATH=/Applications/QGIS.app/Contents/Resources/python
from qgis.core import *

QgsApplication.setPrefixPath("/Applications/QGIS.app/Contents/Resources/python", True)
QgsApplication.initQgis()

layer = QgsVectorLayer('/abs/path/buses.shp', 'buses', 'ogr')
if not layer.isValid():
    print "Layer failed to load: ", layer.lastError() # returns empty string

It seems to me that there is some problem with QGIS data providers in the stand-alone app. Is there a way to find out what exactly happens inside QgsVectorLayer() or get a list of active QGIS data providers?

I am using QGIS 1.8 on OSX 10.8.3 with python2.7 from macports.

share|improve this question
    
When you run your Python code you should see output from QGIS. There should be some lines about loading providers if they fail then something must be setup wrong. –  Nathan W Apr 24 '13 at 22:28
    
Also try setting setPrefixPath to /Applications/QGIS.app/Contents –  Nathan W Apr 24 '13 at 22:29
    
Hm..the MacOS folder did not work for me - I assume because I have a homebrew 2.7 Python installation. But - correct me if I m wrong: I assume that the right prefix path should be the folder that contains the folders: "PlugIns, Resources,svg, ... etc" - right? On top of that, the command print QgsApplication.showSettings() gives me back : Application state: QGIS_PREFIX_PATH env var: Prefix: /Applications/QGIS.app/Contents Plugin Path: /Applications/QGIS.app/Contents/../PlugIns/qgis Package Data Path: /Applications/QGIS.app/Contents/../Resources Active Theme Name: Active Theme Path: :/images/th –  nnneogeorge Oct 29 '13 at 11:09
add comment

2 Answers 2

up vote 2 down vote accepted

Set your prefix path to /Applications/QGIS.app/Contents/MacOS.

You can list the providers available using the providerList method of the provider registry:

from qgis.core import *
QgsApplication.setPrefixPath("/Applications/QGIS.app/Contents/MacOS", True)
QgsApplication.initQgis()
providers = QgsProviderRegistry.instance().providerList()
for provider in providers:
    print provider

To show the current settings, use:

print QgsApplication.showSettings()

This should match what you see in the Log Messages window, General tab in QGIS. If not, adjust your environment accordingly.

share|improve this answer
    
Yes, setting prefix path to /Applications/QGIS.app/Contents/MacOS solves the problem. Thank you! –  srk Apr 25 '13 at 13:53
add comment

But you will encounter many problems because QGIS is made for/uses the standard Apple Python (in/usr/bin) in the Python console and in the Shell and not the MacPorts version, except if you have installed the QGIS version of MacPorts.

share|improve this answer
    
looks like setting prefix path to /Applications/QGIS.app/Contents/MacOS make the app work for both Apple and MacPorts but python versions is something to keep in mind for future troubleshooting –  srk Apr 25 '13 at 14:01
add comment

Your Answer

 
discard

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.