Azitech

Azimout's Linux weblog

Matplotlib in Php

with one comment

You can call Python code from within a Php script and get the return value and output like this:

exec('python mycode.py', $output, $return);
foreach ($output as $line) print "$line<br/>";

To test if everything is ok for your web server to call the python script, try: sudo -u www-data python mycode.py

From this I found out that the Python call import matplotlib was failing with the error:

RuntimeError: ‘/home/azimout’ is not a writable dir; you must set /home/azimout/.matplotlib to be a writable dir.

This can be resolved by adding the following line to your Python code before you import matplotlib: os.environ[ 'HOME' ] = '/tmp/'

Reference: http://www.scipy.org/Cookbook/Matplotlib/Using_MatPlotLib_in_a_CGI_script

About these ads

Written by azimout

21/01/2011 at 10:19

Posted in Problems

One Response

Subscribe to comments with RSS.

  1. THANK YOU! I’ve been debuggins some cgi scripts, and didn’t realize I could specify the server user to run it. I was setting my writeable directory correctly, but the hidden .matplotlib folder was not writable by the server.

    Tim

    30/08/2012 at 21:57


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: