views:

124

answers:

2

I'm having a puzzling problem when trying to import a module in python only when the script is called from php via system or exec.

From the python shell:

import igraph #This works.

if the previous line was in a file, say, test_module.py, then:
python test_module.py in the bash works.

Within PHP:
exec("python test_module.py",$output,$retval) -> fails : $retval = 1.

However, if the script is instead : import math, then this is fine.

Anybody ever dealt with something similar?

A: 

Is the igraph module in Python's standard module path, or is it in the same directory as your individual script? If so, it's quite possible that PHP is calling the python file with a different working directory, and it's trying to import things relative to that path instead of the path of the script.

Amber
Within the python shell:import igraph; print igraph.__file__ returns /usr/lib/python2.4/site-packages/python_igraph-0.5.2-py2.4-linux-x86_64.egg/igraph/__init__.pycThe parent directory /usr/lib/python2.4/site-packages/ is within the sys.path when called from the PHP script. The python_igraph-0.5.2-py2.4-linux-x86_64.egg is also within the sys.path.
Alex Nguyen
Have you tried getting the output from the command (either by specifying the `output` argument for the `exec()` call, or by using `passthru()` instead) to see exactly what error python is giving when called from php?
Amber
I tried doing this. It doesn't seem to output the Python error messages so I have no idea what is going on. It does however have a return value of 1.
Alex Nguyen
I redirected the error message to a file. This may help my sysadmin see what permissions I need.Thanks a lot.
Alex Nguyen
+1  A: 

one thing to check is sys.path

see what the difference is when called each way

cobbal
As commented above. I see no differences between the sys.path.
Alex Nguyen