Saturday, 11 June 2016

pyqgis - Importing QGIS core into Python on OSX


I am interested in learning how to write standalone scripts using QGIS. An answer to my previous question directed me to the PyGIS CookBook which has the following instructions for importing the library in python:


from qgis.core import *


I am using a Mac with OSX 10.7.4. To set the library and pythonpath, I used the following commands in my terminal, based on advice on this mailing list:


export DYLD_LIBRARY_PATH=/Applications/Qgis.app/Contents/MacOS/lib:/Applications/Qgis.app/Contents/Frameworks
export PYTHONPATH=/Applications/Qgis.app/Contents/Resources/python

Then, when I started python and typed from qgis.core import * I got the following error message:


python: VERSIONER_PYTHON_VERSION environment variable error (ignored)
Python 2.6.7 (r267:88850, Jul 31 2011, 19:30:54)
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from qgis.core import *

Traceback (most recent call last):
File "", line 1, in
ImportError: dlopen(/Applications/Qgis.app/Contents/Resources/python/qgis/core.so, 2): Symbol not found: _PyCapsule_Type
Referenced from: /Applications/Qgis.app/Contents/Resources/python/qgis/core.so
Expected in: flat namespace
in /Applications/Qgis.app/Contents/Resources/python/qgis/core.so

Am I setting the paths correctly? I visually checked these and they appear to make sense based on what is in the folder, but I'm not familiar enough with this to know if there is a problem or not.


Edit: In response to @lynxlynxlynx inquiring if a more recent version of python solved the problem, I got a different error message.


When using QGIS, I checked the python version and saw the following message:



>>>sys.version_info
sys.version_info(major=2, minor=7, micro=1, releaselevel='final', serial=0)

In my terminal I get the following message:


>>> sys.version_info
sys.version_info(major=2, minor=7, micro=2, releaselevel='final', serial=0)

If I trying using Python 2.7.2, I get a different error message:


Python 2.7.2 |EPD_free 7.2-2 (32-bit)| (default, Sep  7 2011, 09:16:50) 
[GCC 4.0.1 (Apple Inc. build 5493)] on darwin

Type "packages", "demo", "upgrade" or "enthought" for more information.
>>> from qgis.core import *
Traceback (most recent call last):
File "", line 1, in
ImportError: dlopen(/Applications/Qgis.app/Contents/Resources/python/qgis/core.so, 2): no suitable image found. Did find:
/Applications/Qgis.app/Contents/Resources/python/qgis/core.so: mach-o, but wrong architecture

Answer




Symbol not found: _PyCapsule_Type




QGIS claims to work with python as low as 2.3 and while the PyCapsule module was introduced in 2.7 (as a backport from python 3.1), QGIS itself does not use it directly. The python bindings are generated by SIP during the build phase and if it thought PyCapsule will be available, it created different bindings. It decides by checking if SIP_USE_PYCAPSULE is defined.


You could muck around with recompiling with the symbol manually undefined or an older SIP, but for the first try, I suggest you get a newer version of python (2.7). It could automagically fix your problem.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...