Skip to main content
 首页 » 编程设计

python之导入错误 : No module named http django

2025年05月04日30zlslch

我的 wsgi.py 文件中出现错误。它提示说:

File "(directory)/.local/lib/python2.7/site-packages/Django-1.5 py2.7.egg/django/core/handlers/wsgi.py", line 9, in <module> 
    from django import http 
ImportError: cannot import name http 

我检查了目录 http 存在于 (directory)/.local/lib/python2.7/site-packages/Django-1.5 py2.7.egg/django/中。另外,导入 django.core 时没有问题,但是导入任何其他模块时,都会出现同样的错误。这是 (directory)/.local/lib/python2.7/site-packages/Django-1.5 py2.7.egg/django/的目录信息:

django: 
    bin  conf  contrib  core  db  dispatch  forms  http  __init__.py  __init__.pyc  middleware  shortcuts  template  templatetags  test  utils  views 

这是 http 的目录信息:

http: 
    cookie.py  cookie.pyc  __init__.py  __init__.pyc  multipartparser.py  multipartparser.pyc  request.py  request.pyc  response.py  response.pyc  utils.py  utils.pyc 

编辑:

python shell 中给出的错误:

>>from django import http 
Traceback (most recent call last): 
File "<stdin>", line 1, in <module> 
ImportError: cannot import name http 
 
>>from django import core 
>> 

通过删除也称为 django 的本地目录解决了错误。但是,本地目录已经到位,因为我将我的站点放在共享服务器上,我无法编辑其站点包。有没有办法让应用程序从我的本地和服务器的 django 文件夹中读取,以便我可以使用不在服务器的 django 目录中的模块?

请您参考如下方法:

您可以将您本地的 django 文件夹变成一个命名空间。这告诉 python 解释器继续遍历模块的路径,即使它之前找到了匹配的模块(即你添加的“django”模块)。

将此添加到本地 django 文件夹中的 __init__.py:

import pkg_resources 
pkg_resources.declare_namespace(__name__) 

当 python 找到此模块时,它会运行此代码以将其注册为命名空间。