0

I'm very new at this (haven't even used Terminal before yesterday), but I'm trying to do some terrain mapping following Bjorn Sandvik's instructions at http://blog.thematicmapping.org/2012/07/terrain-mapping-with-mapnik.html. I've installed python and mapnik following the instructions on their respective sites as best I can understand, but when I try to import mapnik via python in Terminal I get an ImportError. Can anyone more experienced tell me how to fix the problem? It looks to me like it's calling a different version of python (if I just type python in Terminal instead of /usr/local/bin/python it returns 2.6), but I don't know what to do about that or where I went wrong in the installation process. I'm on Mac OSX 10.6, 64bit.

$ mapnik-config -v
2.2.0
$ /usr/local/bin/python
Python 2.7.6 (v2.7.6:3a1db0d2747e, Nov 10 2013, 00:42:54) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

>>> import mapnik
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.6/site-packages/mapnik/__init__.py", line 69, in <module> from _mapnik import *
ImportError: dlopen(/usr/local/lib/python2.6/site-packages/mapnik/_mapnik.so, 2): Symbol not found: __tlv_bootstrap
Referenced from: /usr/local/lib/libmapnik.dylib
Expected in: /usr/lib/libSystem.B.dylib
  in /usr/local/lib/libmapnik.dylib
  >>> 

UPDATE: In the hope that it would help (and since I'd been planning to do it anyway), I upgraded my OS to Mavericks, uninstalled all my Python versions from python.org following the instructions here: How to uninstall Python 2.7 on a Mac OS X 10.6.4?, then installed the current version of Xcode (it wasn't available without a $99 paid developer account at Apple for OSX 10.6.8), ran the commands listed below by William again to install homebrew, Python and mapnik, and tried to 'import mapnik' in Python again. Still didn't work, but I have a different error message now.

python
Python 2.7.5 (default, Aug 25 2013, 00:04:04) 
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import mapnik
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named mapnik
>>>

I ran brew doctor, there were far fewer Warnings this time and none seemed surprising. I did take brew doctor's advice and tried resetting my PATH using their recommended code:

echo export PATH='/usr/local/bin:$PATH' >> ~/.bash_profile

That didn't help either. My working directory is now Macintosh:bin home_directory$.

I also noticed an error at the end of the Python install:

Error: Permission denied - /usr/local/lib/python2.7/site-packages/sitecustomize.py

This message re-appeared at the end of the mapnik install. Perhaps neither of these installed properly? When I do mapnik-config -v it says 2.2.0. When I do python --version, it says 2.7.5. When I look in /usr/local/bin I can see these files: mapnik-config, mapnik-speed-check. I do not see any mapnik files when I look in /usr/bin.

UPDATE 2: IT WORKED! I just had to re-do the 'enable python bindings' thing in the mapnik README.txt file (I reset to my home directory first, not sure if that matters). This:

py_version=$(python -c "import sys;print('%s.%s' % (sys.version_info[0],sys.version_info[1]))")
    export PYTHONPATH=/usr/local/lib/python${py_version}/site-packages/:$PYTHONPATH

Seems to be working. Now I just have to fix my mapnik code & I should be ok! Many thanks to those who commented. I don't have enough 'reputation' to vote William's post up so if someone could do that it would be much appreciated!

Community
  • 1
  • 1
cmort
  • 3
  • 4
  • From the looks of it, you haven't installed mapnik correctly. How did you install mapnik? – Burhan Khalid Dec 01 '13 at 11:31
  • Perhaps you installed `mapnik` with the system-Python (`/usr/bin/python`)? – robertklep Dec 01 '13 at 11:34
  • @Burhan Khalid, I just downloaded mapnik and clicked on the install package and went through all the prompts. I also did the following in Terminal per the instructions in the README.txt file for enabling Python bindings: $ py_version=$(python -c "import sys;print('%s.%s' % (sys.version_info[0],sys.version_info[1]))") export PYTHONPATH=/usr/local/lib/python${py_version}/site-packages/:$PYTHONPATH – cmort Dec 01 '13 at 22:29

2 Answers2

1

Even after brew install, you will have to manually export the bindings like this

export PYTHONPATH="/usr/local/lib/python2.7/site-packages"
varun
  • 4,522
  • 33
  • 28
0

You are correct, you have Python versions clashing. Since you are on OSX and you haven't really used terminal before, it is not going to be one 'easy fix'. I recommend you read this book http://linuxcommand.org/tlcl.php, to understand how to use the command line properly. Note: even though it is for Linux, it is for the BASH shell, which is used by default in OSX.

The reason you get Python 2.6 when you type only python is because the directory holding the executable for that version of python is listed before the directory holding the executable for Python 2.7 in the $PATH environment variable. That also means when you installed mapnik it most likely installed that module in the site-packages for Python 2.6 and bound it to that version.

The first thing to check is whether you can import mapnik in Python 2.6. So run python from the terminal (not /usr/local/bin/python). If that works, then that will confirm what I've mentioned above.

The quickest solution is to fix your $PATH environment variable to point to the correct version of python and then re-install mapnik.

The easiest solution, I recommend that you use Homebrew to install Python. Homebrew is a package manager for OSX.

To install Homebrew, in the terminal type:

ruby -e "$(curl -fsSL https://raw.github.com/mxcl/homebrew/go/install)"

Then to install Python:

brew install python

Then to install mapnik;

brew install mapnik

If you use Homebrew, everything should be setup properly, automatically and in theory, there should be no version collisions.

William Denman
  • 3,046
  • 32
  • 34
  • Wow. Quick responses! Thanks. William, I installed homebrew, Python & mapnik like you instructed (I assume that fixes the $PATH environment variable? Or do I have to do that manually beforehand?), but I'm still getting the exact same ImportError when I try to import mapnik. There was a warning from homebrew when I installed mapnik: Xcode is not installed. Could that be part of the issue? I looked up Xcode and it seems I could download an older version for OSX 10.6 with an Apple Developer's Account. Or maybe there is some way to uninstall python & mapnik and start over again using homebrew? – cmort Dec 01 '13 at 20:40
  • I should mention that I did do this at some point during my initial installation per the installation instructions: $ export PYTHONPATH=/usr/local/lib/python${py_version}/site-packages/:$PYTHONPATH – cmort Dec 01 '13 at 20:51
  • It won't necessarily fix the $PATH variable. You don't necessarily need Xcode for mapnik, however you should ry and fix all the messages you get when you run `brew doctor`. Have you re-loaded your bash config? – William Denman Dec 01 '13 at 23:01
  • Ha, I am reading the book you recommended as fast as i can but haven't arrived at how to re-load my bash config yet. I did try this based on another thread here and it didn't work: $ source ~/.bash_profile -bash: export: `/Users/myhome/.bash_profile': not a valid identifier -bash: export: `/Users/myhome/.bash_profile': not a valid identifier – cmort Dec 01 '13 at 23:47
  • to reload your bash config simply close terminal and re-open it again. – Burhan Khalid Dec 02 '13 at 04:06
  • There are two ways to re-load your bash config, without having to close and re-open your terminal. `source ~/.bash_profile`. The source command is just a nice way of doing `. ~/.bash_profile`. These two commands are BASH builtins so the standard `man source` won't give you specific info on them. Try `help source` to see what this particular builtin does. – William Denman Dec 02 '13 at 10:37
  • Thanks @William Denman, as per my Updates above, I was able to use your solution after upgrading to Mavericks and doing a few other things. – cmort Dec 04 '13 at 04:13