Wednesday, January 16, 2013

How to execute python matplolib pylab from PHP

Hey people and Happy new year,

It's crazy try to execute python pylab from an script PHP. After several hours looking for a solution I got it.

If we want to exec a python script from PHP which imports the matplotlib it's necesary to add a couple of extra lines to our code.

# To import pylab
os.environ[ 'MPLCONFIGDIR' ] = '/tmp/'
import matplotlib
matplotlib.use('agg')
import pylab

Before testing your script from the server try to do it on the terminal:

sudo -u www-data php script_name.php

It works for me.

Note: If you want to download and compile matplotlib by yourself, you can download it from:

wget https://downloads.sourceforge.net/project/matplotlib/matplotlib/matplotlib-1.1.1/matplotlib-1.1.1.tar.gz

Decompress , build and execute as root the command:

python setup.py install

Hope help!