Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在windows下从php执行python脚本_Php_Python_Command Line - Fatal编程技术网

在windows下从php执行python脚本

在windows下从php执行python脚本,php,python,command-line,Php,Python,Command Line,好的,这适用于我的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-

好的,这适用于我的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 );
这适用于我的linux机器,但不适用于我的windows测试服务器。如何让它在windows上运行?哦,在windows中直接从命令提示符运行时也可以使用

编辑:

这就是解决方案:

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

我以前使用单引号而不是双引号,将单引号改为双引号解决了这个问题。

我发现
C:\\wamp\\\..
的文件路径在Linux机器上工作很奇怪。 不管怎么说,你有没有试着不逃过刀口

这个问题可能与一些事情有关,包括实现不同程序执行功能的特定于操作系统的方法。我建议您只查看不同的选项,我总是将命令放入一个变量中,然后打印出该变量,这样我就可以看到我将要运行的内容。如果您看到两个斜杠,那么您可能知道没有必要尝试逃避斜杠,但是如果不将它打印到您不知道的屏幕上

尝试浏览其他流程执行函数,看看哪些函数适合您。

我发现最像在命令行运行它的是backtick操作符,然后是
system

试试这样的

$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` 
编辑:啊!我刚刚发现,您的命令周围有一个记号“”,这意味着里面的变量没有被替换。使用双引号代替。这将解决问题,但我仍然建议在运行命令之前回显该命令,这样您就可以看到正在运行的命令,并且能够识别这样的问题

$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` 
在XAMPP服务器中,我的index.py文件如下

我的index.php文件如下


这是一个非常古老的线程,但仍然出现在谷歌的第一个结果中,因此值得更新

2020年,在
Python 3.8
Windows 10
下,正确的解决方案是:

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


其他一切都会导致错误。我花了好几个小时才弄明白。

ahhhh你说得太对了!!一只蜱就杀了它!非常感谢。如何将变量(参数)从php传递到python??如何将变量(参数)从php传递到python??
    Hope this will help you
<?
$output = shell_exec('C:\path\to\pythonscript.py');
print ($output);
?>