找不到名为os的模块 -- Django, mod_wsgi, Apache 2.2

2 投票
1 回答
8695 浏览
提问于 2025-04-16 20:08

我正在尝试设置apache、mod_wsgi和django。我的apache错误日志里出现了一个内部服务器错误,内容如下:

[Wed Jun 22 21:31:55 2011] [error] [client ::1] mod_wsgi (pid=2893): Target WSGI script '/django/internal/django-development.wsgi' cannot be loaded as Python module.
[Wed Jun 22 21:31:55 2011] [error] [client ::1] mod_wsgi (pid=2893): Exception occurred processing WSGI script '/django/internal/django-development.wsgi'.
[Wed Jun 22 21:31:55 2011] [error] Traceback (most recent call last):
[Wed Jun 22 21:31:55 2011] [error]   File "/django/internal/django-development.wsgi", line 3, in <module>
[Wed Jun 22 21:31:55 2011] [error]     import sys, os
[Wed Jun 22 21:31:55 2011] [error] ImportError: No module named os
[Wed Jun 22 21:31:55 2011] [error] [client ::1] mod_wsgi (pid=2893): Target WSGI script '/django/internal/django-development.wsgi' cannot be loaded as Python module.
[Wed Jun 22 21:31:55 2011] [error] [client ::1] mod_wsgi (pid=2893): Exception occurred processing WSGI script '/django/internal/django-development.wsgi'.
[Wed Jun 22 21:31:55 2011] [error] Traceback (most recent call last):
[Wed Jun 22 21:31:55 2011] [error]   File "/django/internal/django-development.wsgi", line 3, in <module>
[Wed Jun 22 21:31:55 2011] [error]     import sys, os
[Wed Jun 22 21:31:55 2011] [error] ImportError: No module named os

django-development.wsgi

#! /usr/bin/env python

import sys, os

path = '/django/internal'
if path not in sys.path:
    sys.path.append(path)

os.environ['DJANGO_SETTINGS_MODULE'] = 'internal.settings'

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

如果有任何我可以提供的信息(比如配置文件),请告诉我,这样你们可以帮助我诊断这个问题。

编辑:

这有点奇怪,因为我以为我把默认的python版本设置成了2.5,并且在运行./configure时把python2.5作为参数传入了。

$otool -L mod_wsgi.somod_wsgi.so:
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 125.2.10)
    /System/Library/Frameworks/Python.framework/Versions/2.6/Python (compatibility version 2.6.0, current version 2.6.1)

还有

LoadModule wsgi_module libexec/apache2/mod_wsgi.so
WSGIScriptAlias / /django/internal/django-development.wsgi

编辑:

啊,看起来是一个错误的配置文件被导入了。现在我得到了以下输出,算是有了一些进展:

[Wed Jun 22 23:04:28 2011] [error] Exception ImportError: 'No module named atexit' in 'garbage collection' ignored
Fatal Python error: unexpected exception during garbage collection
[Wed Jun 22 23:04:28 2011] [error] Exception ImportError: 'No module named atexit' in 'garbage collection' ignored
Fatal Python error: unexpected exception during garbage collection
[Wed Jun 22 23:04:29 2011] [notice] caught SIGTERM, shutting down
[Wed Jun 22 23:04:29 2011] [warn] Init: Session Cache is not configured [hint: SSLSessionCache]
[Wed Jun 22 23:04:29 2011] [warn] mod_wsgi: Compiled for Python/2.5.4.
[Wed Jun 22 23:04:29 2011] [warn] mod_wsgi: Runtime using Python/2.6.1.
[Wed Jun 22 23:04:29 2011] [notice] Digest: generating secret for digest authentication ...
[Wed Jun 22 23:04:29 2011] [notice] Digest: done
[Wed Jun 22 23:04:29 2011] [notice] Apache/2.2.17 (Unix) mod_ssl/2.2.17 OpenSSL/0.9.8l DAV/2 mod_wsgi/3.3 Python/2.6.1 configured -- resuming normal operations
[Wed Jun 22 23:06:04 2011] [error] [client ::1] client denied by server configuration: /django/internal/django-development.wsgi

编辑

所以,我还是想弄清楚怎么让mod_wsgi运行python 2.5,而不是2.6——这正是我现在遇到的大问题。除此之外,应该只需要调整apache的配置。

1 个回答

1

mod_wsgi 可能因为某些原因无法找到你的 Py2.5 库。我建议你检查一下 WSGIPythonPath 的值,确保它指向类似 /usr/lib/python2.5 的路径。

撰写回答