Cyrille
Cyrille

Reputation: 91

Execute python in a php script using shell_exec()

I'm experiencing a weird problem in trying to execute python in a php server (LAMP). (safe_mode off)

if I type:

$output = shell_exec("ls -lah");
echo "<pre>$Output</pre>";

I got the result of the ls command. Same for$output = shell_exec("tar --version"); and other applications, such as gzip.

However, if I switch for any of these lines:

$output = shell_exec("python --version");
$output = shell_exec("python2.7 --version");
$output = shell_exec("/usr/bin/python --version");
$output = shell_exec("python my_script.py");

And other variants of this kind, I get no results. The command is not being executed, the python bitecode not made and the echo remains silent.

I have also tried with the exec() command with no more success.

Upvotes: 9

Views: 28196

Answers (6)

Kamoly
Kamoly

Reputation: 23

I think you need to refer to the full path for your python.

for example use this instead:

$output = shell_exec("/usr/bin/python full_path/my_script.py")

instead of:

$output = shell_exec("python my_script.py");

Upvotes: 1

Suresh Jaiswal
Suresh Jaiswal

Reputation: 1

Most likely the web server doesn't have appropriate rights to execute shell commands. To fix this, run the 'sudo visudo' command and add the following line to the sudoers file:

www-data ALL=NOPASSWD: ALL

Also, make sure that the /var/www directory belongs to the www-data user and group (use sudo chown -R www-data:www-data /var/www to set the correct owner). The details are here http://www.raspberry-pi-geek.com/Archive/2014/07/PHP-on-Raspberry-Pi

Also refer Can't execute python script from php

Upvotes: 0

Chaithanya
Chaithanya

Reputation: 23

If you are trying to run the python script using the following code

$output = shell_exec("python my_script.py");

you will need to use absolute path for my_script.py and give all permissions (I am not sure which ones are sufficient) for the python file.

Upvotes: 1

JDCartee
JDCartee

Reputation: 706

I think this may help...

looks like the output for the python call needs to be routed properly. I was able to make this work within my index.php file for returning the python version...

shell_exec("python -V 2>&1");

Here is where I found the answer.

Upvotes: 11

Phil Perry
Phil Perry

Reputation: 2130

What does

which python

tell you, both from the command line and from shell_exec()? It should tell you which (if any) Python interpreter it's finding (from $PATH). Don't forget that it's quite possible that the $PATH used from the Linux command line might not be the same as the $PATH used by shell_exec()! Once you find the Python interpreter you want to use, you might have to hard code it in the shell_exec().

Upvotes: 0

theartist33
theartist33

Reputation: 482

I think kernel not able to find the path for python where it is installed..if you can do echo $PATH..it will show all the paths where to be search a command if given add your python part there and then it may work or you can give absolute path(other than /usr/bin/) see if it works..I need to test it too.

Upvotes: 0

Related Questions