Skip to main content
 首页 » 编程设计

python之找不到名为 os 的模块——Django、mod_wsgi、Apache 2.2

2025年05月04日1952php

我正在尝试设置 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..,并使用 python2.5 作为参数运行 ./configure

$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 配置。

请您参考如下方法:

mod_wsgi 由于某种原因无法看到您的 Py2.5 库。我建议检查 WSGIPythonPath 的值并确保它指向类似/usr/lib/python2.5 的内容