0

I'm having some dramas with matplotlib and CGI, despite a night spent searching for solutions.

In brief, I'm running Python2.7 with matplotlib through a Bluhost server. I have want a simple script to display an image, but it's getting stuck on the import of matplotlib:

import cgitb, os
cgitb.enable()
import matplotlib

The traceback yields the following:

: No module named matplotlib args = ('No module named matplotlib',) message = 'No module named matplotlib'

Any clues? It seems most of the matplotlib/cgi issues pertain to specification of a writable HOME folder, yet inserting the following doesn't achieve anything:

os.environ[ 'HOME' ] = '/tmp/'

Any ideas or suggestions would be very welcome!

Cheers, Hugh

user2877148
  • 221
  • 1
  • 7
  • It says that it can't find the module, are you sure that `matplotlib` is installed and that it is in the python search path? – tacaswell Oct 14 '13 at 02:02
  • Yes - another important piece of information: I can import matplotlib from the Python prompt on the server, and even run the script, but it seems that I can't execute it from the browser... – user2877148 Oct 14 '13 at 07:38
  • I'm trying to think of a reason why matplotlib can be found while in 'console' mode, but not when executed from a browser. Perhaps there's some extra environment variable I need to specify.... – user2877148 Oct 14 '13 at 07:40
  • I would guess because the server runs as a different user and with a very restricted set of environmental variables. People are going to need more details about your host/web server to give any useful answer. I am 99.999% sure this is an issue with the web-server configuration, not matplotlib. – tacaswell Oct 14 '13 at 14:20
  • Thanks for the tip. Yes, it seems the server is running a different version of Python without linking to the packages I installed on my account. I tried using sys.path.insert(0,'path/to/site-packages/'). Now I can successfully import matplotlib, but it seems there's no inheritance in the import, i.e. it imports matplotlib but won't recognise matplotlib.pyplot, for example. If I import matplotlib.pyplot then it throws errors because it can't find the other dependent packages. Is there a way to forcibly import matplotlib (for example) and everything within it? – user2877148 Oct 14 '13 at 18:50
  • You need to make sure that all of the required libraries are _also_ in the path. Be aware, you are basically doing an end-run around very deliberate security measures. You might do better on superuser with this question. – tacaswell Oct 14 '13 at 18:53
  • OK, so it seems the problem has been solved. There were two issues: 1) The paths to the packages weren't properly specified in the PYTHONPATH belonging to the CGI-version of Python, and 2) The CGI ran a different version of python(p2.6), hence there were some incompatibilities with the various packages. I used "python2.6" when installing the numpy & matplotlib modules to ensure they were compatible with p2.6, then temporarily add a link to the package location when I call my script using CGI. Not ideal, but good enough for starters. Thanks for your help! – user2877148 Oct 15 '13 at 13:36
  • Please post that last comment as an answer and accept it when the system will let you. – tacaswell Oct 15 '13 at 14:33

1 Answers1

0

OK, so it seems the problem has been solved. There were two issues: 1) The paths to the packages weren't properly specified in the PYTHONPATH belonging to the CGI-version of Python, and 2) The CGI ran a different version of python(p2.6), hence there were some incompatibilities with the various packages. I used "python2.6" when installing the numpy & matplotlib modules to ensure they were compatible with p2.6, then temporarily add a link to the package location when I call my script using CGI. Not ideal, but good enough for starters. Thanks for your help!

user2877148
  • 221
  • 1
  • 7