-3

I'm trying to run a Python script from PHP using the following command:

$cmd="sudo /var/www/html/test/class.sh /tmp/tib.jpg"
exec($cmd,$output,$return)

So, i write the command into a shell script "class.sh"

cd $my_work_dir
/usr/bin/python3 -m src.inference.classify file $1

But, i can run the script in command line "php /var/www/html/test/class.sh", but that can't run in the browser with output. I use the proc_open capture the part of error:

 array ( 'stdout' => '', 'stderr' => 'Traceback (most recent call last): File "/usr/lib/python3.4/runpy.py", line 170, in _run_module_as_main "__main__", mod_spec) File "/usr/lib/python3.4/runpy.py", line 85, in _run_code exec(code, run_globals) File "/home/bmk/1\\udce6\\udc96\\udc87\\udce6\\udca1\\udca3/0\\udce7\\udca0\\udc94\\udce5\\udc8f\\udc91/Dog/Dog-AI/dog-breeds-classification-master/src/inference/classify.py", line 7, in import tensorflow as tf File "/usr/local/lib/python3.4/dist-packages/tensorflow/__init__.py", line 24, in from tensorflow.python import * File "/usr/local/lib/python3.4/dist-

I think , because,Python can not locate my python library. I also refered another stackoverflow Running a Python script from PHP , that is not work for me.

Can anybody help me?

Yuntao
  • 17
  • 6
  • this has has been sovled, the key is the python package "six" version. I upgraded my "six" package use "pip install six --upgrade" – Yuntao Dec 18 '17 at 10:18

2 Answers2

0

try

File name should be in path of the php directory i.e where the index.php is present

$a = exec_shell("python filename.py");

Output of $a will be the output of the file.

MdAshiff
  • 151
  • 1
  • 14
0

With exec you can execute php code.

And just include your libary in the right way in the phyton file (maybe fully qualified path name)?

if you want to execute an phyton script form php:

    <?php 

    $runcommand= escapeshellcmd('/usr/custom/test.py');
    $output = shell_exec($runcommand);
    echo $output;

    ?>
Alpix
  • 98
  • 1
  • 9
  • I have followed this , but not work for me. In my python command, i should import the package from relative path. because the browser parse the php with www-data user, this user may be can not rightly locate the packages, i guess. The point is how can make the www-data user get the right packages. I also add the PYTHONPATH in my shell script, but also not work. – Yuntao Dec 14 '17 at 02:51
  • Which os do you have @Yuntao? – Alpix Dec 18 '17 at 07:42
  • ubuntu 14.04, this has has been sovled, the key is the python package "six" version. I upgraded my "six" package use "pip install six --upgrade" – Yuntao Dec 18 '17 at 10:16