Possible Duplicate:
Call Python From PHP And Get Return Code
probably a very stupid question I have a python module lets say hi.py which have the following
print "hi"
now I want to execute this file from php
<?php
#code here
?>
So how do I call that python file from php. Thanks
asked Nov 28, 2011 at 23:34
frazman
33.5k82 gold badges194 silver badges283 bronze badges
-
5stackoverflow.com/search?q=call+python+from+phpNullUserException– NullUserException2011年11月28日 23:36:41 +00:00Commented Nov 28, 2011 at 23:36
2 Answers 2
Use the exec function to invoke Python.
Example:
$output = array();
exec("python hi.py", $output);
var_dump( $output);
There are other commands for executing commands on the machine PHP is running on, see the docs.
answered Nov 28, 2011 at 23:37
nickb
59.7k13 gold badges115 silver badges149 bronze badges
Sign up to request clarification or add additional context in comments.
3 Comments
frazman
when id do the above.. There is no output in the browser.. How do I get that "hi" output on the browser
Jan Vorcak
use
exec("command", $output_array); it's in a docs nickb
@Fraz - I've updated my answer to show you how to capture the output. As Jan Vorcak pointed out, it is in the PHP manual.
you can call php exec function and call
python -c "print 'hi'"
or if you have larger module you can use
-m mod : run library module as a script (terminates option list)
so in the end I'd use
exec("python -m hi");
- and don't forget to configure PYTHON_PATH on your server if needed
- be careful doing this kind of stuff
answered Nov 28, 2011 at 23:38
Jan Vorcak
20.2k15 gold badges57 silver badges91 bronze badges
Comments
default