PHP and Importing pylab

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 (ie, 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.


No complete answer but perhaps helpful:

Assuming you're on Mac OS, I have changed your .php file to to:

<br><br>==== Start ====<br><br>

<?php
    error_reporting(E_ALL);

    $handle = popen('python test.py 2>&1', 'r');
    while (($buffer = fgets($handle, 4096)) !== false) {
        echo $buffer."<br>";
    }
    pclose($handle);

?>

<br><br>==== End ====<br><br>

and the .py file to:

import numpy as np
import matplotlib.pyplot as plt

print "Hello World!"

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:

File "/Library/Python/2.6/site-packages/matplotlib-0.91.1-py2.6-macosx-10.6-universal.egg/matplotlib/ init .py", line 403, in _get_configdir raise RuntimeError("Failed to create %s/.matplotlib; consider setting MPLCONFIGDIR to a writable directory for matplotlib configuration data"%h) RuntimeError: Failed to create /Library/WebServer/.matplotlib; consider setting MPLCONFIGDIR to a writable directory for matplotlib configuration data

Than i did as root:

mkdir /Library/WebServer/.matplotlib
chown _www /Library/WebServer/.matplotlib/

And than the page in the browser contained as last error:

File "/Library/Python/2.6/site-packages/matplotlib-0.91.1-py2.6-macosx-10.6-universal.egg/matplotlib/numerix/ma/ init .py", line 16, in from numpy.core.ma import * ImportError: No module named ma

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).


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',

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

and imported pylab (ie, 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..

import os
os.environ[ 'MPLCONFIGDIR' ] = '/tmp/'

import matplotlib

matplotlib.use('agg')

and it worked...

链接地址: http://www.djcxy.com/p/46714.html

上一篇: 在命令提示符下使用Python matplotlib importError,但不在python shell中

下一篇: PHP和导入pylab