Closed itcarroll closed 7 years ago
Hi @itcarroll, thanks for reporting! Apologies, as the docs in the wiki here are very outdated. For your Python standalone script's shell wrapper script (or environment), you can use:
export HB=$(brew --prefix)
export QGIS_HB_PREFIX=${HB}/opt/qgis2
export QGIS_BUNDLE=${QGIS_HB_PREFIX}/QGIS.app/Contents
export QGIS_PREFIX_PATH=${QGIS_BUNDLE}/MacOS
export PATH=${QGIS_PREFIX_PATH}/bin:$PATH
export PYTHONPATH=${QGIS_HB_PREFIX}/lib/python2.7/site-packages:${HB}/opt/gdal2-python/lib/python2.7/site-packages:${HB}/lib/python2.7/site-packages:$PYTHONPATH
export GDAL_DRIVER_PATH=${HB}/lib/gdalplugins
export GDAL_DATA=${HB}/opt/gdal2/share/gdal
export GRASS_PREFIX=${HB}/opt/grass7/grass-base
After which, you can try this:
from qgis.core import *
qgs = QgsApplication([], False)
qgs.initQgis()
print QgsApplication.showSettings()
shp_file = 'path/to/shape/file.shp'
vlayer = QgsVectorLayer(shp_file, 'vlayer', 'ogr')
print vlayer.isValid()
qgs.exitQgis()
Apparently, QgsApplication.setPrefixPath(<path>)
is not correctly setting the prefix, and thereby not finding/loading any libraries/plugins/providers, hence no authentication system or layers can be loaded. But, setting the QGIS_PREFIX_PATH
env variable does the job; then, you do not need to use QgsApplication.setPrefixPath(<path>)
in your code.
On an odd note, even after setting QgsApplication.setPrefixPath(<path>)
it actually ends up with:
/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS
which is of course waaaay wrong.
Let me know if you need any further explanation on the env variables used here.
Thanks for the additional environment variables! Success!
EDIT: dakcarto fixed code in previous post
Hi @itcarroll, good to hear you have it working. I've update the text of my previous post to reflect your comments. Thanks!
Also, you may want to consider porting your standalone script to QGIS 3, using the homebrew-qgisdev tap. All qgis2*
and qgis3*
formulae can be installed at the same time.
I'm seeing the same issue with QGIS3 and the prefix path. Has anything been done about this or a bug report filed? Also wondering if this is a mac only issue.
Seems to work if you don't create a QgsApplication object:
app = QApplication([])
QgsApplication.setPrefixPath("/Users/gsherman/apps/QGIS.app/Contents/MacOS", True)
QgsApplication.initQgis()
Trying to install qgis2, and have gotten as far as not being able to read a shapefile using PyQGIS from Homebrew python (i.e. a standalone python script). I can read the file just fine with QGIS.app.
I set the PYTHONPATH as advised by
brew info qgis2
:The following script demonstrates the problem.
The message generated at
qgs.initQgis()
follows below, andvlayer
is not valid.Surely there is some additional environment variable to set? I tried using all the exports in the wrapper script
/usr/local/opt/qgis2/bin/qgis2
with no change.