【问题标题】:No module named os found -- Django, mod_wsgi, Apache 2.2找不到名为 os 的模块——Django、mod_wsgi、Apache 2.2
【发布时间】:2011-06-23 04:40:36
【问题描述】:

我正在尝试设置 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 配置。

【问题讨论】:

  • 什么版本的python?你能用普通的python解释器import os吗?
  • 2.5 版——是的,我可以。
  • 能否包含与django-development.wsgi在同一目录中的文件列表?
  • 它实际上是我的整个 django 站点——我应该把它放在一个单独的目录中吗?
  • 您的python安装似乎没有安装os模块。这是您的python安装的问题。您使用的是哪个操作系统和 python?

标签: python django apache mod-wsgi


【解决方案1】:

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

【讨论】:

  • 我把WSGIPythonPath /System/Library/Frameworks/Python.framework/Versions/2.5/bin/python2.5放到httpd.conf中,重启了apache,但结果还是一样(和之前用otool命令的输出一样)。
  • 要明确:WSGIPythonPath 不是 python 二进制文件的路径,它是 python 模块目录的搜索路径。 ':' 分隔列表中的第一个目录应该是其中包含 os.py 的目录。去here了解更多信息。
  • 好的,感谢您的澄清——我已对其进行了修改以包含 /usr/lib/python2.5(并且我检查以确保这是正确的),但它仍然觉得需要运行由于某种原因,使用 2.6。
  • 你能想到它会忽略我的 WSGIPythonPath 的任何其他原因吗?它是否需要高于 httpd.conf 中的特定配置?
  • 不需要。尝试将 wsgi 脚本的第一行设为“import sys; raise Exception(sys.path)”。这将强制它打印您的 python 模块路径/记录它。然后你可以看看它是否加载了正确的目录。
猜你喜欢
  • 2012-07-14
  • 2018-12-21
  • 2014-05-16
  • 2021-11-17
  • 1970-01-01
  • 2018-05-31
  • 1970-01-01
  • 2012-05-13
  • 1970-01-01
相关资源
最近更新 更多