I am using Mapnik v2.2.0 for Python 2.7 (32 bit - Anaconda distribution) at the moment and I wanted to use it in a rudimentary CGI script. The mapnik is definitely installed correctly as it runs without errors if launched straight from Python. However, when I launch one script through another, it gives an

ImportError: No module named mapnik

In other words, consider code:

Script 1:

import CGIHTTPServer, BaseHTTPServer
address = ('', 8000)
handler = CGIHTTPServer.CGIHTTPRequestHandler
server = BaseHTTPServer.HTTPServer(address, handler)

Script 2:

 print '<form method="POST" action="Script3.py">'

Script 3:

import mapnik

Then, accessing launches Script3, which then gives an ImportError.

Is there some compatibility issue about external Python modules being called through CGI?

Is that a problem with Anaconda in particular?

I have partially resolved the issue - copying mapnik to Anaconda directory solved it. HOWEVER, now when the CGI script tries to create a postgis datasource, it returns a Runtime Error. Now, I don't know if PostgreSQL and Postgis somehow bootstrap themselves to the mapnik installation/its original location and trying to call it from what they deem to be a 'different' mapnik fails... Creating the postgis datasource works fine if I do it straight from Python. Thus the problem persists:

Does Anaconda introduce any compatibility/package visibility problems in running its Python scripts as CGI scripts?

1 Answer 1


I resolved the issue more or less fully, and here is what I found - might be useful for future Mapnik/PostGIS users. It's not a perfect solution, but, well, it works.

What I've done is I put the folder with mapnik outside of the folder with Anaconda and just added the path to PYTHONPATH in Python Path Manager at the Anaconda level. HOWEVER, the scripts running externally (like CGI scripts) obviously don't go "through" Anaconda, so they don't see that modification to PYTHONPATH. So first step is to actually get mapnik where it needs to be.

For that, I copied the mapnik python bindings folder (the one inside site-packages) into Anaconda/Lib/site-packages, the main mapnik folder anywhere I want and modified the paths in paths.py in mapnik-v2.2.0/.../site-packages/mapnik to lead to wherever you put the main mapnik folder at. After relaunching Anaconda, everything worked.

I THINK if one just added the correct path to mapnik to PYTHONPATH (rather than going through Anaconda's manager), it would work as well. Haven't tested it myself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.