如何让PHP认识到本地安装Python模块用于执行Python脚本

如何让PHP认识到本地安装Python模块用于执行Python脚本

问题描述:

我有以下的Python code,以后将在以后使用PHP来调用:

I have the following Python code, which later will be called using PHP later:

#!/usr/bin/env python    
"""Code name: test.py"""
import BeautifulSoup

import sys
myargs = sys.argv[1]

mylist = ["foo bar qux", "uix nan col"]
for chunk in mylist:
    mems = chunk.split()
    mems.append(myargs)
    print mems

和模块BeautifulSoup安装在本地的位置:

And the module BeautifulSoup is installed locally here:

$ echo $PYTHONPATH
/home/pdubois/.local/lib/python2.7/site-packages

现在这是PHP code调用它

Now this is the PHP code that calls it

    <HTML>
    <HEAD>
    <TITLE>MYCODE</TITLE>
    </HEAD>
    <BODY>
    <BR>

     <?php echo '<p>This is the output of my code:</p>'; ?>
     <?php
         exec(`python /misc/path_to_mycode/test.py param1`,$output);
        var_dump($output)
     ?>

   </BODY>
   </HTML>

的index.php code以上居住在的/ var / www / html等

The index.php code above resides at /var/www/html.

结果我得到的是这样的:

The result I get is this:

array(4) { [0]=> string(34) "Traceback (most recent call last):" 
[1]=> string(65) " File "/u21/pdubois/my_project/test.py", line 15, in " 
[2]=> string(24) " import BeautifulSoup" 
[3]=> string(42) "ImportError: No module named BeautifulSoup" }

当我注释掉进口BeautifulSoup 在test.py. PHP的code返回正确的预期结果:

When I comment out import BeautifulSoup in test.py. The PHP code returns the correct expected result:

array(2) { [0]=> string(31) "['foo', 'bar', 'qux', 'mcpip1']" 
[1]=> string(31) "['uix', 'nan', 'col', 'mcpip1']" }

问题是我应该怎么做才能让PHP / Python的/阿帕奇承认BeautifulSoup或任何本地安装的模块?

The question is what should I do to make PHP/Python/Apache recognize the BeautifulSoup or any locally installed modules?

在显示BeautifulSoup的目录出现在你的家目录。但我怀疑Apache正在运行,你:最有可能它正在运行为 WWW 。所以,你需要安装的模块,该用户或全局。

The directory you show for BeautifulSoup appears to in your home dir. But I doubt that Apache is running as you: most likely it is running as www. So you'd need to install the module for that user, or globally.

当然,一个更好的办法是坚持一种语言:如果你想运行Python,然后写在一个Python框架的网站;或者使用PHP的HTML解析器。

Of course, a much better approach would be to stick to one language: if you want to run Python, then write the website using a Python framework; alternatively use a PHP HTML parser.