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;
?>
Related
I'm creating a webpage in PHP wherein if a button is pressed, the PHP script executes a python script using shell_exec() command. The python script creates a pdf file using fpdf python library. When I run the same from command shell, it works prefectly but when I try to run python script from PHP, it gives me nothing. Here is PHP code:
<!doctype html>
<?php
$command = escapeshellcmd('/home/amogh/server/test.py');
$output = shell_exec($command);
echo $output;
?>
Python script:
#! /usr/bin/python
#print ('hello')
from fpdf import FPDF
fp = open('downloads/boot.txt', 'r')
pdf = FPDF()
pdf.add_page()
pdf.set_font('Arial', '', 11)
line = fp.read()
pdf.multi_cell(200, 5, line, 0, 1)
pdf.output('test.pdf', 'F')
Also, I'm using lighttpd server. Can anyone tell me where am I going wrong?
I'd guess the working directory is wrong and your script can't access the file it needs.
You may want to use
shell_exec("/home/amogh/server/test.py 2>&1");
in order to redirect stderr to stdout, so any error output from Python will be also in the $output variable, to help you debug things.
I have the following python script test.py:
import subprocess
subprocess.call(["php", "C:/Documents and Settings/user/My Documents/Downloads/load_data.php"])
the idea is to run another php script in which I make a LOAD DATA INFILE to a MySQL table, but it generates the following error:
Traceback <most recent call last>:
File "test.py", line 3, in <module>
subprocess.call<["php","load_data.php"]>
File "C:\Python34\lib\subprocess.py", line 537, in call
with Popen<*popenargs, **kwargs> as p:
File "C:\Python34\lib\subprocess.py" line 859, in __init__
restore_signals, start_new_session>
File "C:\Python34\lib\subprocess.py", line 1114, in _execute_child startupinfo>
FileNotFoundError: [WinError 2] The system cannot find the file specified
If I run the direct load_data.php script in a web browser, it works without any problem so it seems to me that the problem is in python, it is worth mentioning that I have done this correctly before but with python 3.6, some idea if I need it configure something or add a library.
I have php5.3, apache 2.2.21, mysql5.5.20, python3.4, I tried to put the absolute path of the PHP script but is the same
Regards!
For run PHP with Python, you must add the full path to the PHP executor and the full path of the file to execute.
Example :
import subprocess
subprocess.call(["C:\\wamp64\\bin\\php\\php5.3\\php.exe", "C:\\Documents and Settings\\user\\My Documents\\Downloads\\load_data.php"])
Don't forget to escape the anti-slashes, Otherwise the subprocess will not work and there will be no visible error.
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');
I have made a python script for getting a directory tree. Now I am trying to call this script from php using system().
My Python Script is:
import os
from sys import argv
script, start = argv
def list_files(startpath):
directory = []
for root, dirs, files in os.walk(startpath, topdown=True):
for named in dirs:
directory.append(os.path.join(root, named))
for namef in files:
directory.append(os.path.join(root, namef))
return directory
dir = list_files(start)
My PHP command is:
$var = system('C:\\Python27\\python.exe generate -tree.py F:\\IET', $retval);
echo $var;
echo $retval;
The output that I am getting on the browser is the number '2', and when I was doing it a day before the output was coming as the number '1'.
I am new to PHP so couldn't really understand what I am doing wrong here.
Any Guidance would be appreciated!
I am trying to call a php script from python script using subprocess:
cmd1 = 'php -f ' + path + '/src/MarketplaceWebService/Samples/ReviseItem.php %s %s' % (sku,qty)
print cmd1
args1 = shlex.split(cmd1)
p=subprocess.Popen(args1)
p.wait()
But i am getting the followwing error:
PHP Fatal error: Class 'MarketplaceWebService_Client' not found in
/home/nish/stuff/repos/new/voylla_staging_changes/voylla_scripts/amazon/src/MarketplaceWebService/Samples/ReviseItem.php
on line 70
When I run the same php script via command line using php -f ReviseItem.php, it works fine without any errors. How can i fix this error.
the include() or require() calls in your php scripts are using relative file paths, which are themselves relatives to the directory you actually are in when you run the command.
You can assert this with a echo getcwd(); call at the beginning of your php script, for example.
So, you have to either modify your python code to cd to the right dir prior to running the command (easier), or modify your php scripts to use absolute paths for inclusion (not the best way to go since your scripts would be tied to a specific directory on the system).