I have a PHP script that calls in a python program. Here is the php script:
<?php
$last_line = popen('/Library/Frameworks/Python.framework/Versions/Current/bin/python test.py', 'r');
$results = fgets($last_line);
print $results;
?>
and this is the content of test.py:
test.py:
import numpy as np
from matplotlib.patches import Ellipse
# import matplotlib.pyplot as plt
# from matplotlib.pyplot import figure, show
# import pylab
print "Hello World!"
Now, this works fine and I get "Hello World!" in browser. However, if I uncomment any of the imports (i.e., import matplotlib.pyplot as plt, import matplotlib.pyplot as plt or import pylab) I don't get the result from PHP. It will be great if someone could help me with this as I need all the plotting functions from Python.
Thanks Jan Kuiken for trying it out! Finally after hard struggle I was able to figure it out. First as suggested in this page, I added the following to 'test.py',
and imported pylab (i.e., uncommented it).
This worked from the command line but not in the browser. Then I did a passthru and got the following error in the browser:
dyld: Symbol not found: __cg_jpeg_resync_to_restart Referenced from: /System/Library/Frameworks/ApplicationServices... Expected in: /Applications/MAMP/Library/lib/libjpeg.62.dylib
I figured out it should be a problem with ImageMagick and followed the suggestions from the following page: Getting MAMP 1.9 to work with ImageMagick
And after correct installation, I was able to get the python code going on. The python code had a command to plot and save the figure and I guess ImageMagick is required in that case!
The matplotlib part was not getting executed when the python script was executed through php. Then i just include the following statements in my .py file..
and it worked...
No complete answer but perhaps helpful:
Assuming you're on Mac OS, I have changed your
.php
file to to:and the
.py
file to:The
2>&1
redirects the error output of programs to the standard output, this can usefull for debugging purposes.The result on the page in the browser contained:
Than i did as root:
And than the page in the browser contained as last error:
hmm, thats still disappointing, however you're mileage may vary, the problem has probably to do with the environment/permissions of the webserver user (_www).