调用python脚本*从php导入非标准库* [英] call python script *with non-standard library imported* from php

查看:127
本文介绍了调用python脚本*从php导入非标准库*的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经搜索了所有相关问题,有很多类似的问题,但是我的问题是

I have searched thru all related questions, there are lots of similar ones, but my question is

如何从php调用一个python脚本,导入了一些非标准库/模块

how to call, from php, a python script in which I imported some non-standard libraries/modules

更具体地说,我有一个.py文件test.py,我在其中导入了pycurl和bottlenose,这是一个Amazon API包装器。这两个模块都不在标准库中。因此,文件看起来像是

To be more specific, I have a .py file, test.py, in which I imported pycurl and bottlenose, which is an amazon API wrapper. Both module are not in the standard library. So the file looks like

import pycurl
import bottlenose
...
print "hello!"

然后我制作了一个php文件,test.php

Then I made a php file, test.php

<?
$output = array();
exec("/home/my_user_name/local/Python-2.7/bin/python test.py", $output, $ret_code);
var_dump( $output);
echo $ret_code;
?>

test.php和test.py都在同一目录下。更重要的是,如果我运行

Both test.php and test.py are under the same directory. More importantly if I run

php test.php 

在我的终端机上,它没有问题。它成功输出 hello,并且返回代码为0,这意味着程序退出时没有错误

in my terminal, there was no problem with with it. It sucessfully output "hello", and return code is 0, which means program exits with no error

但是,当我在浏览器上对其进行测试 >,www.someurl.com / test.php,发生了一些错误。我逐行跟踪,发现错误发生在导入pycurl和导入宽吻处。

However, when I tested it on my browser, www.someurl.com/test.php, some error occured. I tracked line by line and found the error occurs at the "import pycurl" and "import bottlenose".

我还尝试打印出sys.path,它确实包含了瓶颈路径。

I also tried to print out sys.path, it does include the path of bottlenose.

有人可以吗?

谢谢!

推荐答案

我会信任系统如果您在调用Python程序之前在php脚本中调用putenv,则.path是正确的。

I would trust the sys.path being correct if you were invoking putenv in the php script before calling the Python program.

成功运行后,请把握机会打印sys.path。让我们将此打印结果称为 x。然后,尝试以下操作:

From the successful run, take the chance to print the sys.path. Let us call the result of this print as 'x'. Then, try the following:

putenv('PYTHONPATH=x');
exec("/home/my_user_name/local/Python-2.7/bin/python test.py", $output, $ret_code);

有帮助吗?

这篇关于调用python脚本*从php导入非标准库*的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆