Sunday, 8 September 2013

Import astropy calling Python from PHP

Import astropy calling Python from PHP

I'd like to run a Python script from PHP (LAMP). The script imports
astropy, and for some reason I am not able to successfully do that. I can
reproduce the basic problem with the example files copied below. I don't
think it is a permission or Python path problem. I can import numpy,
scipy, etc., for example. I can also create a Python script to read and
write files on that same directory (/www/var). I have the standard Python
on /usr/bin/python and also the Anaconda version. Both have astropy and I
get the same problem regardless of which one I use.
I suspect it is something about running the code as myself (which works
fine) or as www-data (from the web browser). However, when I do this from
the Linux terminal: sudo -u www-data php test.php, the code works as
expected.
This is test.php:
<?php
echo "<p>begin test.php</p>";
system("python test.py");
echo "<p>end test.php</p>";
?>
And test.py:
print("this is test.py ...")
import sys, numpy, scipy, matplotlib
print("passed the import sys, numpy, scipy, matplotlib ...")
import astropy
print("passed the import astropy ...")
What I get when I open test.php is this:
begin test.php
this is test.py ... passed the import sys, numpy, scipy, matplotlib ...
ERROR: TypeError: coercing to Unicode: need string or buffer, NoneType
found [genericpath]
end test.php

No comments:

Post a Comment