2

I am trying to run a python script from php.

exec("../cgi-bin/form.py", $output);
var_dump($output);

I'm certain the path is correct, and that form.py is executable.

this is form.py

#!/usr/bin/env python
print "IN form.py'

However, this prints out NULL. I don't think the script is being executed. How do I make sure it is?

6
  • If you type which python from the command line, it should give you the full path to the python executable. You can then use that path and enter it at the top of your form.py script. Commented Jun 7, 2012 at 18:23
  • i don't have shell access :(. Commented Jun 7, 2012 at 18:46
  • You can use php exec to run the command, I suspect the return value will be /usr/bin/python. Commented Jun 7, 2012 at 18:57
  • @patyx7 echo exec("which python"); returns nothing. Commented Jun 7, 2012 at 19:06
  • How about exec("/usr/bin/which python", $output); ? Commented Jun 7, 2012 at 19:19

1 Answer 1

4

You're literally just typing in the location of the file. You need to tell exec to execute python with that script.

exec("python ../cgi-bin/form.py", $output);
Sign up to request clarification or add additional context in comments.

4 Comments

If he specifies the correct path to python within his script and has made sure his script is executable, then it shouldn't be necessary to add python to the exec command :)
Very true, but it's fair to assume it's not executable since he wasn't getting output to begin with.
@MarcusRecck so i added headers to the form.py and i added this link to the php: echo('<a href="../cgi-bin/form.py">CLICK</a>'); which works! so directory is correct and the script runs fine. also, adding python did not do anything. still outputting null
I understand this is old, but, for Windows and possibly other machines, make sure python is located on your path.

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.