我正在尝试从 PHP 文件中调用 Python 脚本,但是当我必须加载本地库时它会失败。如果我的 PHP 不加载本地库,我的 PHP 能够调用我的 python,并且我的 python 脚本在手动启动时可以工作。
以下是最小(非)工作文件:
index.php
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
</head>
<body>
<?php
$command = escapeshellcmd ("./getter.py") ;
$output = shell_exec ($command) ;
echo "<p>$output</p>" ;
?>
</body>
</html>
getter.py
#!/usr/bin/env python2
import got
if __name__ == "__main__" :
print "Python2 working"
got
当地著名的图书馆在哪里。
当我手动启动./getter.py
打印时,我的网页没有显示任何内容。当我评论时import got
,网页也显示打印。
附加信息 :
- 操作系统:Ubuntu 18.04.1
- 服务器网站:nginx 1.14.0
- PHP : 7.2
- 蟒蛇:2.7.15rc1
- 我是网络语言的新手,所以我还不知道调试工具
- 导入非本地库(如 、 或其他)时
os
没有csv
问题 - 我试图
$command = escapeshellcmd ("./getter.py") ; $output = shell_exec ($command) ;
用一个简单的替换$output = shell_exec ("./getter.py") ;
- 我试图替换
#!/usr/bin/env python2
为#!/usr/bin/python2
- 当我要求 PHP 或 Python 给我它们当前的工作目录时,它们都
/var/html/www/test
按计划返回而不是它们所在的位置。
这是一个文件夹树:
├── getter.py
├── got
│ ├── __init__.py
│ ├── manager
│ │ ├── __init__.py
│ │ ├── TweetCriteria.py
│ │ └── TweetManager.py
│ └── models
│ ├── __init__.py
│ └── Tweet.py
├── index.php
├── __init__.py
└── lib
├── __init__.py
└── toto.py
非常感谢。