简体   繁体   中英

executing python script from php under windows

Okay this works with my linux server but I'm testing my web site at home on my PC and am trying to get php to execute my python script and of course the \\ are WINDOWS only the path changes when Im on my linux machine.

$pyscript = 'C:\\wamp\\www\\testing\\scripts\\imageHandle.py';
$python = 'C:\\Python27\\python.exe';
$filePath = 'C:\\wamp\\www\\testing\\uploads\\thumbs\\10-05-2012-523.jpeg'


exec('$python $pyscript $filePath', $output, $return );

this works on my linux machine but not my windows testing server. How do I get this to run on windows? Oh and this also works when running directly from the command prompt in windows

EDIT:

This is the solution:

exec("$python $pyscript $filePath", $output);

I used single quotes instead of double quotes before, changing the single quotes to double quotes fixed the problem.

I find it odd that the filePath of C:\\\\wamp\\\\.... would work on a Linux machine. Anyway, have you tried NOT escaping the slashes?

The issue can be related to several things, including OS specific methods of implementing the different program execution functions. I recommend just going through the different options, I always put the command into a single variable and then print out the variable so that I can see what I am about to run, if you saw two slashes, then you might know that there is no need to try to escape the slashes, however without printing it to the screen you wouldn't know.

Try going through the other process execution functions to see what works for you. http://www.php.net/manual/en/ref.exec.php

The one that I have found works MOST like running it at the command line, is the backtick operator, and then next is system

try something like this.

$pyscript = 'C:\\wamp\\www\\testing\\scripts\\imageHandle.py';
$python = 'C:\\Python27\\python.exe';
$filePath = 'C:\\wamp\\www\\testing\\uploads\\thumbs\\10-05-2012-523.jpeg'

$cmd = "$python $pyscript $filePath";
echo $cmd;
`$cmd`


$pyscript = 'C:\wamp\www\testing\scripts\imageHandle.py';
$python = 'C:\Python27\python.exe';
$filePath = 'C:\wamp\www\testing\uploads\thumbs\10-05-2012-523.jpeg'

$cmd = "$python $pyscript $filePath";
echo $cmd;
`$cmd` 

EDIT: AHHH! I just figured it out, you are surrounding your command with single ticks '' which means the variable inside are not being replaced. Use double quotes instead. That WILL solve the problem, but I still recommend echoing the command out before you run it so that you would see the command you are running and you would be able to identify an issue like this.

This is a very old thread, but still comes up among the first results in Google, so it is worth an update.

In 2020, Under Python 3.8 and Windows 10 the right solution is simply:

<?
$output = shell_exec('C:\path\to\pythonscript.py');
print ($output);
?>

Everything else will result in an error. Took me a good few hours to figure out.

In XAMPP SERVER my index.py file be like

from wordsegment import load, segment
load()
print(segment('morerecentlythedevelopmentwhichisapotent'))

my index.php file be like

<html>
<head>
  <title>py script</title>
</head>

<body>
  <h1>Hey There!Python Working Successfully In A PHP Page.</h1>
  <?php
    $python = `python index.py`;
    echo $python;
    ?>
</body>
</html>

Now run the index.php file.

 Hope this will help you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM