繁体   English   中英

PHP exec 在执行 Python 脚本时返回空字符串

[英]PHP exec returns empty string while executing Python script

我正在尝试在 PHP 中运行 Python 脚本并显示输出。

我尝试在 PHP 中使用简单的 test.py,它可以毫无问题地打印 hello world。 但是当我尝试从 PHP 脚本执行我想要的命令时, exec()返回空字符串而不是输出。

所以这里是我的 php 脚本。 下面的片段工作正常:

$output = exec("python test.py"); 
var_dump($output);

但不是想要的。

$command = "python -m scripts.label_image --graph=tf_files/retrained_graph.pb --image=".$uploadfile;
$output = exec($command);
var_dump($output);

所以文件夹结构是:

/var/www/mysite/
              -scripts(folder which contains python scripts)
              -tf_files(folder with other files)
              -uploads (image folder)

有人可以告诉我出了什么问题吗?

stdout/shell 中的示例输出:

python脚本的shell输出

变量 $output 应该是一个参数。 如果这样做,您应该只获得脚本的最后一个输出。

exec("python test.py", $output); 
var_dump($output);

https://www.php.net/manual/en/function.exec.php

暂无
暂无

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

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