Global name 'mapnik' not defined when using Tilestache

1.5k views Asked by At

I set up a TileStache server on Redhat, installing Mapnik 2.2 from source. However, Tilestache is giving me the following error:

Traceback (most recent call last):
  File "/usr/lib64/python2.6/site-packages/gevent/pywsgi.py", line 508, in handle_one_response
    self.run_application()
  File "/usr/lib64/python2.6/site-packages/gevent/pywsgi.py", line 494, in run_application
    self.result = self.application(self.environ, self.start_response)
  File "/usr/lib/python2.6/site-packages/TileStache/__init__.py", line 381, in __call__
    status_code, headers, content = requestHandler2(self.config, path_info, query_string, script_name)
  File "/usr/lib/python2.6/site-packages/TileStache/__init__.py", line 254, in requestHandler2
    status_code, headers, content = layer.getTileResponse(coord, extension)
  File "/usr/lib/python2.6/site-packages/TileStache/Core.py", line 414, in getTileResponse
    tile = self.render(coord, format)
  File "/usr/lib/python2.6/site-packages/TileStache/Core.py", line 500, in render
    tile = provider.renderTile(width, height, srs, coord)
  File "/usr/lib/python2.6/site-packages/TileStache/Goodies/Providers/MapnikGrid.py", line 72, in renderTile
    self.mapnik = mapnik.Map(0, 0)
NameError: global name 'mapnik' is not defined

Relevant Information:

  • Other posts have suggested changing 'import mapnik' to 'import mapnik2 as mapnik'. But I got the same error message.
  • In other posts originates from TileStace/Mapnik.py, but mine comes from TileStache/Goodies/Providers/MapnikGrid.py.
  • Related Post: Gunicorn fails when using WSGI

Question:

Does anyone know what could be causing this? Thanks in advance!

2

There are 2 answers

0
AudioBubble On BEST ANSWER

It turns out the root of this problem is fairly common. The libmapnik shared library was not found.
When I tried to import mapnik from the python console I received this error:

>>> import mapnik
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib64/python2.6/site-packages/mapnik/__init__.py", line 69, in <module>
    from _mapnik import *
ImportError: libmapnik.so.2.2: cannot open shared object file: No such file or directory 

Which led me to the answer here: https://github.com/mapnik/mapnik/wiki/InstallationTroubleshooting#the-libmapnik-shared-library-is-not-found

Solution: Add '/usr/local/lib' to '/etc/ld.so.conf' and run ldconfig.

0
elrobis On

...keeping the Aug 22 flow in 2018 with Mapnik 3 and TileStache, but getting a nearly identical error. The accepted answer didn't help me, and it turns out Mapnik Python bindings are installed separately in more recent versions.

Looking here, I was able run from the command line, simply:

pip install mapnik

And that fixed me up so that I could run the python interpreter and import mapnik.

One potential caveat. That Mapnik news page mentions the pip install makes use of "Python Wheels", and if you check the linked page, it's mentioned that you'll need pip >= 1.4. You can check your Pip with pip --version on the command line, and mine was pip 7.1.2. So chances are this is a non-concern for most people.