4

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.

asked Apr 24, 2013 at 22:17
3
  • 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. Commented Apr 24, 2013 at 22:28
  • Also try setting setPrefixPath to /Applications/QGIS.app/Contents Commented Apr 24, 2013 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 Commented Oct 29, 2013 at 11:09

2 Answers 2

4

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.

answered Apr 25, 2013 at 2:51
1
  • Yes, setting prefix path to /Applications/QGIS.app/Contents/MacOS solves the problem. Thank you! Commented Apr 25, 2013 at 13:53
1

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.

answered Apr 25, 2013 at 7:01
1
  • 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 Commented Apr 25, 2013 at 14:01

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.