Python Local Package Doesn't Load In Call From PHP
So, I want to run a Python program with a home-directory install of PyProj from PHP. The PHP and Python are simple, but I include them below for completeness.
I've tested running Python manually using both sys.path.append
and PYTHONPATH
to specify the location of the package. Both of these methods work.
However,开发者_如何学JAVA when I shell_exec
the script from PHP, I'm told ImportError: No module named pyproj
.
A recursive check of the file system reveals that everything is read/executable by user, group, and other.
Any thoughts on why this I can't get this to run?
I'm calling it in a PHP script as follows
<?php
putenv('PYTHONPATH="/home/userperson/public_html/lib64/python2.4/site-packages"');
$ret=shell_exec("./bob");
print $ret;
?>
The Python program is simple.
#!/usr/bin/python
import pyproj
import sys
sys.path.append("/home/userperson/public_html/lib64/python2.4/site-packages")
surfproj = pyproj.Proj(proj='lcc',lat_1=40,lat_2=50,lon_0=-95,lat_0=40,ellps='WGS84')
x,y=surjproj(-95,45)
print x
A good way to solve a problem like this is to print the sys.path
from a Python script in this environment and check what the current path is:
#!/usr/bin/python
import sys
print sys.path
My guess is that it will containt '"/home/userperson/public_html/lib64/python2.4/site-packages"'
(note the superfluous double quotes).
The documentation of putenv($setting)
doesn't say anything about supporting shell syntax, quotes or escaping inside the setting, so any characters present in the strings would no doubt end in the value of the environmental variable. A possible fix for the issue would be:
putenv('PYTHONPATH=/home/userperson/public_html/lib64/python2.4/site-packages');
Another useful hint would be to put the path in a separate variable, and just do putenv("PYTHONPATH=$pythonpath")
or putenv("PYTHONPATH=" . implode(':', $pythonpath))
, as this would allow you to check if the paths exist from your PHP script with file_exists
.
Older versions of PHP might have issue if safe_mode
is enabled and PYTHONPATH
isn't in the safe_mode_allowed_env_vars
, but hopefully you're not running on a server configured like that.
精彩评论