本文介绍了Django循环导入错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个django新手。我刚刚在windows vista上安装了v.1.1.1(使用setup.py安装)for python 2.5



当我启动一个python shell并尝试导入django.db时,我得到以下循环导入错误

 >>> import os 
>>> os.environ ['DJANGO_SETTINGS_MODULE'] ='设置'
>>> import django.db
追溯(最近的最后一次调用):
文件< stdin>,第1行,< module>
文件C:\Python25\lib\site-packages\django\db\__init __。py,第78行,< module>
connection = connections [DEFAULT_DB_ALIAS]
文件C:\Python25\lib\site-packages\django\db\utils.py,第93行,__getitem__
backend = load_backend(db ['ENGINE'])
文件C:\Python25\lib\site-packages\django\db\utils.py,第33行,在load_backend
return import_module('。base',backend_name)
文件C:\Python25\lib\site-packages\django\utils\importlib.py,第35行,import_module
__import __(name)
文件C:\Python25\Lib\site-packages\django\db\backends\sqlite3\base.py,第14行<模块>
from django.db import utils
ImportError:无法导入名称utils
>>>

看看代码,我可以看到 django\db\ backends\sqlite3\base.py import django\db\utils.py ,但是这个文件还会导入base.py使用import_module)。由于循环导入不一定会崩溃?



另一方面,如果我从 python manage.py shell中使用shell 一切工作正常,所以必须有一些东西可以运行在我的简单的shell上使其工作



感谢任何提示!



编辑:



Delyan提出了两种可能的解决方案:

  C:\Users\xulo> cd c:\django_example 
c:\django_example> cd mysite
c:\django_example\ mysite> python
在win32
上,Python 2.5(r25:51908,Sep 19 2006,09:52:17)[MSC v.1310 32位(Intel)]键入help,copyright 信用或许可证获取更多信息。
>>> import os
>>> os.environ ['DJANGO_SETTINGS_MODULE'] ='设置'
>>>导入sys
>>>> sys.path.append('c:\\\django_example\\\mysite')
>>> sys.path.append('c:\\\django_example')
>>>从django import db
>>>>

  c:\django_example\mysite> python 
Python 2.5(r25:51908,Sep 19 2006,09:52:17)[MSC v.1310 32位(Intel)]在win32
输入help,copyright,credits或license了解更多信息。
>>>导入设置
>>>导入django.core.management
>>> django.core.management.setup_environ(settings)
'c:\\django_example\\mysite'
>>>从django import db
>>>>

这两个都很好,但是现在我会打开这个问题,看看有没有一个简单的解释为什么,以及为什么排序在utils.py和base.py之间的明显的循环导入

解决方案

这很烦人,但Django希望您在 sys.path 中将项目的文件夹的父项。您可以在 setup_environ 中看到这种情况发生在 django.core.management .__ init __



最近发生了一个问题,这可能会在不久的将来重构,但现在只需将这两个文件夹添加到任何自定义脚本(尽管您应该真正将它们添加为管理。 py 命令)。



编辑:这是。


I'm a django newbie. I just installed v 1.3.1 on windows vista (using setup.py install) for python 2.5

When I start a python shell and try to import django.db I get the following circular import error

>>> import os
>>> os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
>>> import django.db
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python25\lib\site-packages\django\db\__init__.py", line 78, in <module>
connection = connections[DEFAULT_DB_ALIAS]
  File "C:\Python25\lib\site-packages\django\db\utils.py", line 93, in __getitem__
backend = load_backend(db['ENGINE'])
  File "C:\Python25\lib\site-packages\django\db\utils.py", line 33, in load_backend
return import_module('.base', backend_name)
  File "C:\Python25\lib\site-packages\django\utils\importlib.py", line 35, in import_module
    __import__(name)
  File "C:\Python25\Lib\site-packages\django\db\backends\sqlite3\base.py", line 14, in <module>
    from django.db import utils
ImportError: cannot import name utils
>>>

Looking at the code, I can see that django\db\backends\sqlite3\base.py imports django\db\utils.py, but then this file also imports base.py (using import_module). Isn't that necessarily going to crash due to circular import?

On the other hand, if I use the shell from python manage.py shell everything works fine, so there must be something I can run on my plain shell to make it work

Thanks for any hints!

EDIT:

Delyan came up with two possible solutions:

C:\Users\xulo>cd c:\django_example
c:\django_example>cd mysite
c:\django_example\mysite>python
Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
>>> import sys
>>> sys.path.append('c:\\django_example\\mysite')
>>> sys.path.append('c:\\django_example')
>>> from django import db
>>>

or

c:\django_example\mysite>python
Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import settings
>>> import django.core.management
>>> django.core.management.setup_environ(settings)
'c:\\django_example\\mysite'
>>> from django import db
>>>

Both work well, but I'll leave the question open for now to see if someone has a simple explanation of why, and why that sorts the apparent circular import between utils.py and base.py

解决方案

It's rather annoying but Django wants you to have your project's folder and its parent in sys.path. You can see this happening in setup_environ in django.core.management.__init__

There was an issue raised recently and this might be refactored in the near future but for now just add those two folders to any custom scripts (though you should really be adding them as manage.py commands).

Edit: This has been partially refactored in Django 1.4.

这篇关于Django循环导入错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 10:15