3

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, 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
1
  • What happens if you import sys first, then print sys.path? Does the output include the directory that contains your module? Commented Aug 15, 2011 at 19:21

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Good call. There were superfluous quote marks in the path, so that's working using putenv now. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.