9

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.

Cyrille
  • 91
  • 1
  • 1
  • 2

6 Answers6

11

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.

JDCartee
  • 696
  • 6
  • 9
  • I also had the same error with `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.. – Raynal Gobel Sep 01 '15 at 10:51
  • Worked for me too, but specifically for me, running in shared hosting managed with CPanel. My py scripts are below the /public_html root, and using $out = shell_exec('python ../pyscr/script.py '.param1.' 2>&1'); puts the print values from script.py into $out. – Bitfool Apr 05 '16 at 02:45
1

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.

ConcurrentHashMap
  • 4,998
  • 7
  • 44
  • 53
Chaithanya
  • 23
  • 4
1

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");
Kamoly
  • 23
  • 7
0

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.

theartist33
  • 470
  • 1
  • 6
  • 13
  • it is also possible that ls and gzip are working because these are system commands..what you are using is exclusive or other command..will let you know once I test it on my hp ux box – theartist33 Sep 10 '13 at 14:09
  • Hi! Thanks for your fast reply! I have tried echo $PATH /usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin:/usr/share/java/apache-ant/bin:/usr/bin/vendor_perl:/usr/bin/core_perl The python interpreter is located in the /usr/bin/ so it should find it, no? – Cyrille Sep 10 '13 at 14:25
0

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().

Phil Perry
  • 2,126
  • 14
  • 18
  • Hi Phil! `which python` gives exactely the same answer from both the command line and php: `/usr/bin/python`. The problem may not come from this... – Cyrille Sep 10 '13 at 18:23
  • BTW, `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:/bin` – Cyrille Sep 10 '13 at 18:25
0

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

Community
  • 1
  • 1