Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matplotlib and WSGI/mod_python not working on Apache

Everything works as supposed to on the Django development server. In Apache, the django app also works except when matplotlib is used. Here's the error I get:

No module named multiarray.

Exception Type: ImportError 
Exception Value: No module named multiarray 
Exception Location: /usr/share/pyshared/numpy/core/numerictypes.py in <module>, line 81 
Python Executable: /usr/bin/python 
Python Version: 2.6.4 

From the python shell, both statements work: import numpy.core.multiarray and import multiarray. Any ideas? Thanks

As I'm looking over the numpy files, I found the multiarray module, which has an extension of 'so'. My guess, is that mod_python is not reading these files.

like image 457
Luiz C. Avatar asked Apr 01 '10 15:04

Luiz C.


2 Answers

Problem solved. Here's what I did.

First of all, before I was getting the import error:

"No module named multiarray."

I was getting an error like this:

": Failed to create /some/dir/.matplotlib; consider setting MPLCONFIGDIR to a writable directory for matplotlib configuration data"

By adding the pyshared folder to the PythonPath variable, this error went away and I got the import error.

So here's how I fixed it:

Removed the pyshared folder from the PythonPath variable.

Before importing the matplotlib module, add these lines:

import os
os.environ['HOME']='/some/writable/dir'

Next, before import matplotlib.pyplot or pylab, add these lines:

import matplotlib
matplotlib.use('Agg') 
# 'Agg' or whatever your backend is.

This is documented here.

That's is! It's working on python2.5 for me now. But I believe it'll work on 2.6 as well.

like image 82
Luiz C. Avatar answered Nov 07 '22 20:11

Luiz C.


On Win32 I solved a similar problem (not being able to load pyd modules through ISAPI_WSGI (IIS)) by downgrading from py2.6.5 to py2.5. It seems like this might be a Python bug that has been re-introduced. See for example this discussion.

like image 38
e1i45 Avatar answered Nov 07 '22 20:11

e1i45