I am trying to run a Python Script for Image Detection Via a php page and to catch and display the output generated by py script.
Here is my python script GITHUB LINK
I am running the python script using this code :
$command = escapeshellcmd('sudo python Main.py');
$output = shell_exec($command);
echo $output;
This is producing this error :
Traceback (most recent call last):
File "Main.py", line 3, in <module>
import cv2
ImportError: No module named cv2
****But the same script works fine when executed independently with terminal.
I have used anaconda for installation of Opencv****
Thanks in advance :)
Anaconda creates separate environment so you need to start your script using python copy from that enviroment
$command = escapeshellcmd('sudo /home/path_to_anacondad_env/python Main.py');
Related
Trying a very simple task. Call python example.py from a php script from a windows xampp server.
Notes:
- Both files are in the same directory.
- Going to http://example.com/example.py works fine.
- Going throught the command line (cd inside where example.py is then (python example.py)) works also.
example.py
#! C:{path-to-python.exe}
print("Content-Type: text/html\n")
print("<html>")
print("<h1>Something.</h1>")
index.php
<?php
$command = escapeshellcmd('python example.py');
$output = shell_exec($command);
echo $output;
?>
Nother note:
- If I use $command = escapeshellcmd('example.py'); it opens the python script in my text editor.
Someone tell me plz what am I doing wrong. Thank you.
to call python script from php :
$result=exec("python test.py ");
if you want to passing parameters to python script :
$result=exec("python test.py param1 param2");
in python script if you want get parameters:
import sys
param1=sys.argv[1]
param2=sys.argv[2]
note : $result in php taking first print statement in python script
I'm getting a import error for spacy when executing new_api.py through PHP. The python script gets successfully executed when run through the cmd
PHP Code
<?php
$json = file_get_contents('php://input');
$input = json_decode($json, TRUE);
$result = shell_exec('/usr/bin/python3 new_api.py "'.$input['queryResult']['queryText'].'" "'.$input['session'].'" 2>&1');
$output = json_encode(array(
"source" => "source",
"fulfillmentText" => $result
));
print_r($output);
?>
Snippet Of Python Code
import requests
import json
import sqlite3
import spacy
import sys
json_values = sys.argv[1:]
result = (home(json_values[0],json_values[1]))
print(result)
The Output/Error Traceback
{"source":"source","fulfillmentText":"Traceback (most recent call last):\n File \"new_api.py\", line 4, in \n import spacy\nImportError: No module named 'spacy'\n"}
There's two version of python in the server. Both the versions has spacy installed in it and the python script is compatible with both versions.
Things I've tried
Tried using exec() and shell_exec() in php
Tried using which python path to execute the script
Tried running the script which both python and python3
The ImportError Still persists even after trying the combinations of the above steps. Any help would be appreciated.
Probably the user of the server who execute the PHP script does not have enough permissions to access the module.
You can install the module as server user or use venv
I have same problem, and I soleved it just a moment ago. This question is old, but I leave a note for people has same problem, and for me.
"printenv" on shell (your own user).
exec('printenv') from php (probably "apache" user).
Check difference between 1 and 2 results.
In my case, LD_LIBRARY_PATH=/usr/local/pgsql/lib is needed (exist in 1 and not exist in 2). So I modified my code as follows:
exec('LD_LIBRARY_PATH=/usr/local/pgsql/lib python3 test_script.py 2>&1');
I have tried exec() and system() command to run the python script. Nothing seems to be working.
Basically, I'm using localhost to host a webpage. In the same system, there is a python script which I want to run using PHP(from a web page through a button). I do not want python code to be called to PHP to run, I want to run PHP code in the same system using the web interface(PHP)
PYTHON CODE:
import os
from os import mkdir, makedirs
bobo = r"C:\\AmeyoS3-Automated\\bobo_folder"
os.makedirs(bobo)
PHP CODE 1(Didn't work):
system("C:\Program Files (x86)\Python37-32\python.exe C:/Users/Administrator/Desktop/ameyo-selenium/temp/bobo.py")
PHP CODE 2(Didn't work):
$python = "C:\Program Files (x86)\Python37-32\python.exe";
$file = "C:\Users\Administrator\Desktop\ameyo-selenium\temp\bobo.py";
$cmd = "$python $file";
exec("$cmd");
Python script works fine while running independently.
Problem is running the script through PHP Command.
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?
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.
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;
?>
I have a python code which runs in my terminal and prints the result. I wrote a php code which executes the python code using exec("python example.py argument_1"). The php code also prints the result while executing it from the terminal.
But when I try to call the php from the browser (or) through a curl request, the result of the php is not being displayed. Apache error log displays the following message.
Traceback (most recent call last):
File "./example.py", line 3, in <module>
from lxml import etree
ImportError: No module named lxml
Kindly look into this issue.
Solution :
I had conflict with the python versions
exec("/usr/local/bin/python example.py argument_1") has resolved my issue
I think you can print the sys.path(modules will be fond by python via those path ) for more details. you maybe develop your code in linux.
In linux, each user has different sys.path.
for example, I use root to run python to get sys.path in python,
'/usr/bin',
'/usr/lib/python2.7',
'/usr/lib/python2.7/plat-x86_64-linux-gnu',
'/usr/lib/python2.7/lib-tk',
'/usr/lib/python2.7/lib-old',
'/usr/lib/python2.7/lib-dynload',
'/usr/local/lib/python2.7/dist-packages',
'/usr/lib/python2.7/dist-packages',
'/usr/lib/python2.7/dist-packages/PILcompat',
'/usr/lib/python2.7/dist-packages/gtk-2.0',
'/usr/lib/python2.7/dist-packages/ubuntu-sso-client',
'/usr/lib/python2.7/dist-packages/IPython/extensions']
use an other user to get sys.path in python
['',
'/home/mahome/anaconda2/bin',
'/home/mahome/anaconda2/lib/python27.zip',
'/home/mahome/anaconda2/lib/python2.7',
'/home/mahome/anaconda2/lib/python2.7/plat-linux2',
'/home/mahome/anaconda2/lib/python2.7/lib-tk',
'/home/mahome/anaconda2/lib/python2.7/lib-old',
'/home/mahome/anaconda2/lib/python2.7/lib-dynload',
'/home/mahome/anaconda2/lib/python2.7/site-packages',
'/home/mahome/anaconda2/lib/python2.7/site-packages/Sphinx-1.4.1-py2.7.egg',
'/home/mahome/anaconda2/lib/python2.7/site-packages/setuptools-23.0.0-py2.7.egg',
'/home/mahome/anaconda2/lib/python2.7/site-packages/IPython/extensions',
'/home/mahome/.ipython']
so, you maybe use different user to execuate your code.