1

I have this code in php:

$var = "something";
$result = exec("python /somePath/someFile.py $var");
print $result;

and my someFile.py is like this:

import functions as f

x = f.removeSpace("hey   there")
print x 

Now, without the import functions everything is ok, but when I try to import another module then php screws. I have included sys.stderr = sys.stdout in my python file, and when I run my php code then it returns:

AttributeError: 'module' object has no attribute 'removeSpace'

Any suggestions?

3 Answers 3

2

I had a problem like this recently. It turned out that PHP's exec wasn't passing environment variables through to the program it executed, so Python wasn't seeing $PYTHONPATH, and didn't know where to import the module from.

I don't know how to fix that from PHP, but in our case, symlinking the imported package to the same directory as the script made it work.

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

Comments

1

Check your functions module, see if you have defined an removeSpace function.

It's not a PHP issue, it's Python issue, you may have forgotten to define that function.

2 Comments

well the python file works perfectly when I execute it normally
You said: Now, without the import functions everything is ok, but when I try to import another module then php screws. not when I execute it normally as you said in this comment!
0

I don't really know about python, but I think the problem is someFile.py can't find your functions-module. Because you are executing it from PHP, it's pwd will be set to php's pwd, not /somePath/.

Comments

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.