0

I have various version of python installed in my server, I want to call Python2.6 and I'm calling it by this:

echo shell_exec("python2.6 /root/python/test.py 2>&1")

and it is not executing and giving me following error:

sh: python2.6: command not found 

when I try to run this command in console it works fine but not when I call it via PHP, I also tried running only "python /root/python/test.py" and it works fine.

Hoyo
  • 1,044
  • 3
  • 13
  • 23
  • where is binary "python2.6" and "python" – Kamil Karkus Feb 09 '15 at 07:55
  • @kmlnvm I'm not a server guy but I installed them on their usual locations, means I did not change any location or anything after installing.. – Hoyo Feb 09 '15 at 07:59
  • Have a look at this: http://www.csh.rit.edu/~jon/projects/pip/ – Gaurav Dave Feb 09 '15 at 08:00
  • Try using the full path, like `shell_exec("/usr/bin/python2.6...` (run `which python2.6` in your console to find out where exactly it is). – georg Feb 09 '15 at 08:04
  • In terminal type "which python" . This will give you the path of python. Apply it in your shell_exec() – fortune Feb 09 '15 at 08:08
  • @georg thanks it worked! now I'm having Permission denied to write a file – Hoyo Feb 09 '15 at 08:08
  • Thanks for @georg I got it working but now I'm having permission denied to run the script. usr/local/bin/python2.6: can't open file '/root/python/test.py – Hoyo Feb 09 '15 at 08:15
  • @Hoyo: in Unix, you always run programs as some "user", and the same physical person (you) can be different "users". In your console you are "root" (bad idea, BTW), in your php script you are "wwwdata" or something like that and "wwwdata" doesn't have access to "root"'s files. – georg Feb 09 '15 at 08:18
  • @georg then how can I run the file as root? or should I just create a sh file and do it though it? – Hoyo Feb 09 '15 at 08:21
  • @Hoyo: sorry, I can't guide you step-by-step through the basics of Unix. Please, get some literature and educate yourself. – georg Feb 09 '15 at 08:24

2 Answers2

0

I needed to add the complete python's version binary when calling the python2.6 in PHP.

 echo shell_exec("/usr/local/bin/python2.6 /root/python/test.py 2>&1")
Hoyo
  • 1,044
  • 3
  • 13
  • 23
0

Why don't you execute your python script directly? It need to add Shebang(#!) to your python script, and mark it as executable(chmod +x). After - you would run script, for example like:

echo shell_exec(__DIR__ . '/test.py');

Should I put #! (shebang) in Python scripts, and what form should it take?

Community
  • 1
  • 1
Sergey Chizhik
  • 617
  • 11
  • 22