简体   繁体   English

在windows下从php执行python脚本

[英]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.好的,这适用于我的 linux 服务器,但我正在家里的 PC 上测试我的网站,并试图让 php 执行我的 python 脚本,当然 \\ 是 WINDOWS 只有当我在我的 linux 机器上时路径会发生变化。

$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.这适用于我的 linux 机器,但不适用于我的 Windows 测试服务器。 How do I get this to run on windows?如何让它在 Windows 上运行? Oh and this also works when running directly from the command prompt in windows哦,这也适用于直接从 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.我觉得奇怪的是C:\\\\wamp\\\\....的 filePath 可以在 Linux 机器上工作。 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 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我发现最像在命令行中运行它的方法是反引号运算符,然后是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: 2020 年,在Python 3.8Windows 10 ,正确的解决方案很简单:

<?
$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在 XAMPP SERVER 中,我的 index.py 文件就像

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

my index.php file be like我的 index.php 文件就像

<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.现在运行 index.php 文件。

 Hope this will help you

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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