0

I am trying to run a simple python script from within my php application.

For test purposes, here is my code:

function run()
{
    $result = exec('python3.7 testPy.py',$output, $return_code); 
    dd($result, $output, $return_code); //dd() is alias for dump and die. 
} 
run();

testPy.py:

print('Ok') 

if I run this command via console as root I get the following output:

root@xxx:Controller$ python3.7 testPy.py 
Ok 

The same thing is with the www-data user:

www-data@xxx:Controller$ python3.7 testPy.py 
Ok 

How ever, if call my php script via browser, I get the following output:

run.php on line 3: "" 
run.php on line 3: []
run.php on line 3: 2 

run.php and testPy.py are in the same folder.

Any ideas are very welcome.

5
  • 1
    change to print('Ok') ? Commented Oct 24, 2019 at 10:11
  • Thank for noticing, was copy paste error when preparing for this post. Commented Oct 24, 2019 at 10:18
  • Possible duplicate of Python exit codes Commented Oct 24, 2019 at 10:26
  • Try $result = exec('python3.7 ./testPy.py',$output, $return_code); Commented Oct 24, 2019 at 10:27
  • No result change. Commented Oct 24, 2019 at 11:10

2 Answers 2

0

I noticed on some hosting servers the exec function in php is disabled by default. Check your phpinfo()

or

if(function_exists('exec')) {
    echo "exec is enabled";
}
Sign up to request clarification or add additional context in comments.

Comments

0

Try this code in php file

$command = escapeshellcmd('python testPy.py');
$output = shell_exec($command);
echo $output;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.