2

I am trying to run a python script from php and access a mysql database it works completely when i use

$python sample.py

but on using

$output=shell_exec('python sample.py');

echo $output

it doesn't execute the python script though .php has the permissions

$sudo chmod +x sample.php

$sudo chown www-data:www-data sample.php

$sudo chmod u+x sample.php
Ali AzG
  • 1,861
  • 2
  • 18
  • 28
abr_98
  • 33
  • 3
  • Does it give you any output? Like an error message perhaps? – Priyank Dec 08 '18 at 07:47
  • see normal output in a file first if you are running on linux $output=shell_exec('python sample.py > file.txt'); and the see what written on file.txt – Ahmed Abdelazim Dec 08 '18 at 07:48
  • in database it does not give any output. I ahve tried adding simple print statements also but they are also not getting executed.It is returning an empty text file file.txt – abr_98 Dec 08 '18 at 07:50
  • Can you have a read of https://stackoverflow.com/questions/19735250/running-a-python-script-from-php and see if that helps. – Nigel Ren Dec 08 '18 at 07:52
  • thanks but i have already tried all the commands there it doesn't work – abr_98 Dec 08 '18 at 08:11

2 Answers2

0

Enter exact syntax as you put it on terminal

$output=shell_exec('python sample.py'); or

$output=shell_exec('python -i sample.py');

or make it executablechmod +x sample.py

then $output=shell_exec('./sample.py');

Ahmed Abdelazim
  • 717
  • 7
  • 14
0

So you can try this non-direct method

shell_exec('python sample.py > output.txt'); $output = file_get_contents('output.txt','r'); echo $output; shell_exec('rm output.txt');

Ahmed Abdelazim
  • 717
  • 7
  • 14