PHP and Importing pylab
Question: 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.


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

