将模块加载到 apache 中mod-wsgi
我正在使用 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.py
与 index 是同一目录.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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我发现我必须添加几行来附加 python 路径。类似于:
对于第二部分,只需为导入目录添加额外的行。即:
要查找导入目录,请在交互式 python 提示符中输入:
I find that I have to add a few lines to append the python path. Something like:
for the second part, just add additional lines for your import directories. ie:
to find your import directories, type into an interactive python prompt:
其他答案集中于让脚本本身破坏其自己的 PYTHONPATH。另一种方法是找出正确的 Apache 设置,为 Python 和 WSGI 应用程序设置可行的路径。
我的conf文件中有这些:
如果您不想传递环境的PYTHONPATH,我想您可以使用:
我建议您尝试一下。
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:
If you don't want to pass your environment's PYTHONPATH, I think you can use:
I suggest you give these a shot.
sys.path
和sys.modules
检查该目录是否实际添加为模块目录。如果不是sys.path.append
它。sys.path
andsys.modules
to check whether the directory is actually added as a module directory. If notsys.path.append
it.