2

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 23351500)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from qgis.core import *
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
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 "<stdin>", line 1, in <module>
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
PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Jul 22, 2012 at 19:51

1 Answer 1

3

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.

answered Jul 22, 2012 at 20:35
4
  • hmmm, I was trying to use an earlier version of python as I get a wrong architecture when I use python 2.7. I'll update my question to include that error message too. Commented Jul 22, 2012 at 20:44
  • Isn't your system 64-bit (that's what wikipedia says lion supports only)? Commented Jul 22, 2012 at 21:03
  • interesting, yes I just reinstalled python 2.7.3 and that fixed it! Not sure I fully understand why... but thanks for the assistance. Commented Jul 22, 2012 at 21:16
  • 2
    You had a 32-bit python 2.7 installed before and you can't mix and match programs compiled for different architectures that easily. Commented Jul 22, 2012 at 21:18

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.