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.
-
2possible duplicate of PHP and shell_execcjdcordeiro– cjdcordeiro2013年09月10日 14:08:26 +00:00Commented Sep 10, 2013 at 14:08
-
Maybe a right problem ?Freelancer– Freelancer2013年09月10日 14:09:29 +00:00Commented Sep 10, 2013 at 14:09
-
I've read this post in detail as many other, i didn't find any answer thereCyrille– Cyrille2013年09月10日 14:44:45 +00:00Commented Sep 10, 2013 at 14:44
-
Have you solved this?user966588– user9665882013年10月09日 14:26:10 +00:00Commented Oct 9, 2013 at 14:26
-
stackoverflow.com/questions/18689684/… This could be a answer for this.user966588– user9665882013年10月09日 14:27:43 +00:00Commented Oct 9, 2013 at 14:27
6 Answers 6
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.
2 Comments
pdftk. But it explains why. "/opt/lampp/lib/libstdc++.so.6: version `GLIBCXX_3.4.9' not found (required by pdftk)". This may be the case with python? Let me experiment a bit..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.
Comments
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");
Comments
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.
2 Comments
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().
2 Comments
which python gives exactely the same answer from both the command line and php: /usr/bin/python. The problem may not come from this...shell_exec('echo $PATH'); gives the same answer in the console and the terminal: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/binMost 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