将模块加载到 apache 中mod-wsgi

发布于 2024-10-02 05:58:30 字数 1541 浏览 2 评论 0原文

我正在使用 Apache + mod-wsgi。

在我的 httpd.conf 中,文件末尾有以下附加行。

LoadModule wsgi_module modules/mod_wsgi-win32-ap22py27-3.3.so
WSGIScriptAlias / "C:/Projects/Folder/web/"
<Directory "C:/Projects/Folder/web">
AllowOverride None
Options None
Order deny,allow
Allow from all
</Directory>

当我通过 http://localhost/script/ 在 Windows 中执行以下 index.py 脚本时index.py

def application(environ, start_response):
    status = '200 OK' 
    output = 'Hello World!'
    response_headers = [('Content-type', 'text/plain'),
                        ('Content-Length', str(len(output)))]
    start_response(status, response_headers)
    return [output]

工作得很好。

但是,当我在 index.py 的第一行添加 import utils 时,我得到

ImportError: No module named utils

utils.pyindex 是同一目录.py

我需要设置任何额外的配置吗?

我尝试了 @dan_waterworth 给出的建议,

import sys, os
sys.path.append(os.path.dirname(__file__))

通过导入我自己的模块,我不再遇到错误。但是,当我导入正在通过 easy_install 安装的模块时,会发生错误。

   File "C:/Projects/Folder/web/script\\connection.py", line 1, in <module>
     import psycopg2
   File "build\\bdist.win32\\egg\\psycopg2\\__init__.py", line 65, in <module>
     from psycopg2 import tz
 ImportError: cannot import name tz

如果此脚本作为独立应用程序执行,则 import psycopg2 执行没有问题。

I am using Apache + mod-wsgi.

In my httpd.conf, I am having the following additional lines at the end of file.

LoadModule wsgi_module modules/mod_wsgi-win32-ap22py27-3.3.so
WSGIScriptAlias / "C:/Projects/Folder/web/"
<Directory "C:/Projects/Folder/web">
AllowOverride None
Options None
Order deny,allow
Allow from all
</Directory>

When I execute the following index.py scripts in Windows through http://localhost/script/index.py

def application(environ, start_response):
    status = '200 OK' 
    output = 'Hello World!'
    response_headers = [('Content-type', 'text/plain'),
                        ('Content-Length', str(len(output)))]
    start_response(status, response_headers)
    return [output]

Works pretty fine.

However, when I add import utils at the first line of index.py, I get

ImportError: No module named utils

utils.py is same directory as index.py

Is there any additional configuration I need to set?

I try suggestion given by @dan_waterworth

import sys, os
sys.path.append(os.path.dirname(__file__))

I get no more error by importing my own module. However, when I import module which is being installed through easy_install, error happens.

   File "C:/Projects/Folder/web/script\\connection.py", line 1, in <module>
     import psycopg2
   File "build\\bdist.win32\\egg\\psycopg2\\__init__.py", line 65, in <module>
     from psycopg2 import tz
 ImportError: cannot import name tz

import psycopg2 executed no problem, if this script is being executed as standalone application.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

稳稳的幸福 2024-10-09 05:58:30

我发现我必须添加几行来附加 python 路径。类似于:

import sys, os
sys.path.append(os.path.dirname(__file__))

import utils

对于第二部分,只需为导入目录添加额外的行。即:

sys.path.append([enter path here])

要查找导入目录,请在交互式 python 提示符中输入:

import sys
print sys.path

I find that I have to add a few lines to append the python path. Something like:

import sys, os
sys.path.append(os.path.dirname(__file__))

import utils

for the second part, just add additional lines for your import directories. ie:

sys.path.append([enter path here])

to find your import directories, type into an interactive python prompt:

import sys
print sys.path
百善笑为先 2024-10-09 05:58:30

其他答案集中于让脚本本身破坏其自己的 PYTHONPATH。另一种方法是找出正确的 Apache 设置,为 Python 和 WSGI 应用程序设置可行的路径。

我的conf文件中有这些:

PassEnv PYTHONPATH
WSGIPythonHome  C:/Python/Python26
WSGIPythonPath  C:/Python/Python26;C:/myproject/PyLib

如果您不想传递环境的PYTHONPATH,我想您可以使用:

SetEnv PYTHONPATH C:/your/paths/go/here;C:/and/here

我建议您尝试一下。

The other answers focus on getting the script itself to mangle its own PYTHONPATH. Another approach is to figure out the right Apache settings that will setup a workable path for Python and WSGI apps.

I have these in my conf file:

PassEnv PYTHONPATH
WSGIPythonHome  C:/Python/Python26
WSGIPythonPath  C:/Python/Python26;C:/myproject/PyLib

If you don't want to pass your environment's PYTHONPATH, I think you can use:

SetEnv PYTHONPATH C:/your/paths/go/here;C:/and/here

I suggest you give these a shot.

千秋岁 2024-10-09 05:58:30

sys.pathsys.modules 检查该目录是否实际添加为模块目录。如果不是sys.path.append它。

sys.path and sys.modules to check whether the directory is actually added as a module directory. If not sys.path.append it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文