Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to run python From PHP

I would like to run a few python scripts(I am aware of the risks , but I HAVE to get it done)

i tried using :

echo exec('python --version ');

as well as echo shell_exec('python --version ');

Also tried '/usr/bin/python ' instead of just python but I dont get any output at all. I have even added the www-data to the sudoers list, still not working.

What should I do ?

Running debian and python 2.7

like image 634
harveyslash Avatar asked Apr 29 '26 08:04

harveyslash


1 Answers

It seems python --version prints version info to stderr instead of stdout for some reason, so you'll need to redirect former to latter:

exec('python --version 2>&1');

Also, note that exec's return value is just the last line of the executed command's output. If you want to catch full output from command that returns multiple lines, you'll need to provide an array as exec's second argument:

$output = array();
exec($some_command, $output);
like image 132
lafor Avatar answered Apr 30 '26 22:04

lafor