[mod_python] Matplotlib error from a mod_python script "root is not a writable dir... "

Graham Dumpleton graham.dumpleton at gmail.com
Sat Feb 21 18:51:01 EST 2009


2009/2/22 Ron Phelps <rphelps at redshift.com>:
> I have successfully configured mod_python and using some python scripts
> am accessing a database for the purpose of generating plots using
> matplotlib.
>
> The problem is that when I try to save a plot to a subdir of the Apache
> document root I get the following error.
>
> '/root' is not a writable dir; you must set the environment variable
> HOME to be a writable dir
>
> echo $HOME  returns '/root' but setting it to the correct path and even
> changing the dir permissions does no good.
>
> Tried useing the MPLCONFIGDIR to point to a MATPLOTLIBRC file but no
> luck there either.
>
> Does anyone know What HOME variable is this error message referring to?
>
> BTW Graham, I came across your article on "modwsgi" though it may not be
> directly related I will read it in hopes it gives me some insight.

The following environment variables can/will all be set wrong if using
mod_python.

  HOME
  LOGNAME
  USERNAME
  USER

If any Python code uses those directly, or indirectly via
os.path.expanduser() function, then the results will be wrong.

The only way that is guaranteed to give correct values are to use
os.geteuid() and then 'pwd' module to look up user details in password
database.

Even then, the result will then reflect what is correct for the user
that Apache runs as. The home directory of that account isn't usually
writable either.

You thus need to find a way of telling matplotlib to look use
alternate location. You appear to have tried that, but the question is
how have you tried to do that? You cannot use SetEnv or PythonOption
directives as they do not set environment variables in Apache server
child processes.

What you do to do is set os.environ dictionary explicitly in your
mod_python handler entry point module, or possibly in a special module
imported using PythonImport directive.

Graham


More information about the Mod_python mailing list