Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 检测和纠正循环导入_Python_Django - Fatal编程技术网

Python 检测和纠正循环导入

Python 检测和纠正循环导入,python,django,Python,Django,所以,这是我上周所问问题的扩展(),但经过大量调试后,我觉得这可能需要一个新问题 我有许多模块,都带有导入。我怀疑可能有循环导入,但我一直未能找到它 奇怪的是: 在一个models.py文件中,我有: from django.db import models from django.core.exceptions import ObjectDoesNotExist from heythere.models import Notification as HeyThere from heythere

所以,这是我上周所问问题的扩展(),但经过大量调试后,我觉得这可能需要一个新问题

我有许多模块,都带有导入。我怀疑可能有循环导入,但我一直未能找到它

奇怪的是:

在一个models.py文件中,我有:

from django.db import models
from django.core.exceptions import ObjectDoesNotExist
from heythere.models import Notification as HeyThere
from heythere.models import NotificationManager as HeyThereManager
from django.contrib.auth.models import User
然后是一些模型定义,包括带有方法的定义:

def to_user(self):
    try:
        return User.objects.get(username=str(self.username))
    except ObjectDoesNotExist:
        return None
再往下一点:

from ownership.apps.Assets.models import Item
当它是这样的时候,我得到了AttributeError:'NoneType'对象没有属性'objects'(指用户)。因此,不知何故,它被设置为None,而不是使用未定义的
全局名称XXX

但是,我尝试在导入后立即访问用户:

me = User.objects.get(username='xxxx')
print me
它在运行
runserver
时打印正确,因此我知道它可以访问那里的用户,但随后给了我错误消息
ImportError:cannotimport name Item
。简单地访问模型是如何导致导入错误的?(FWIW,
确实与
用户
有许多FK关系)

有什么好处

包含
User.objects.get()
时的回溯:

  File "/Users/xxxxx/.virtualenvs/ownership/lib/python2.7/site-packages/django/utils/autoreload.py", line 93, in wrapper
    fn(*args, **kwargs)
  File "/Users/xxxxx/.virtualenvs/ownership/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 101, in inner_run
    self.validate(display_num_errors=True)
  File "/Users/xxxxx/.virtualenvs/ownership/lib/python2.7/site-packages/django/core/management/base.py", line 310, in validate
    num_errors = get_validation_errors(s, app)
  File "/Users/xxxxx/.virtualenvs/ownership/lib/python2.7/site-packages/django/core/management/validation.py", line 34, in get_validation_errors
    for (app_name, error) in get_app_errors().items():
  File "/Users/xxxxx/.virtualenvs/ownership/lib/python2.7/site-packages/django/db/models/loading.py", line 196, in get_app_errors
    self._populate()
  File "/Users/xxxxx/.virtualenvs/ownership/lib/python2.7/site-packages/django/db/models/loading.py", line 78, in _populate
    self.load_app(app_name)
  File "/Users/xxxxx/.virtualenvs/ownership/lib/python2.7/site-packages/django/db/models/loading.py", line 99, in load_app
    models = import_module('%s.models' % app_name)
  File "/Users/xxxxx/.virtualenvs/ownership/lib/python2.7/site-packages/django/utils/importlib.py", line 40, in import_module
    __import__(name)
  File "/Users/xxxxx/code/ownership/ownership/apps/Assets/models.py", line 12, in <module>
    import permissions
  File "/Users/xxxxx/code/ownership/ownership/apps/Assets/permissions.py", line 4, in <module>
    from ownership.apps.Shared.models import Person
  File "/Users/xxxxx/code/ownership/ownership/apps/Shared/models.py", line 87, in <module>
    from ownership.apps.Assets.models import Item
ImportError: cannot import name Item

请在我的堆栈跟踪中注意:

 File "/Users/xxxxx/code/ownership/ownership/apps/Assets/models.py", line 12, in <module>
    import permissions   File "/Users/xxxxx/code/ownership/ownership/apps/Assets/permissions.py", line 4, in <module>
    from ownership.apps.Shared.models import Person   File "/Users/xxxxx/code/ownership/ownership/apps/Shared/models.py", line 87, in <module>
    from ownership.apps.Assets.models import Item
文件“/Users/xxxxx/code/ownership/ownership/apps/Assets/models.py”,第12行,在
导入权限文件“/Users/xxxxx/code/ownership/ownership/apps/Assets/permissions.py”,第4行,在
从ownership.apps.Shared.models导入个人文件“/Users/xxxxx/code/ownership/ownership/apps/Shared/models.py”,第87行,在
从ownership.apps.Assets.models导入项目
Assets/models.py
正在导入
权限

权限
正在导入
共享.models.Person

Shared/models
正在导入
Assets.models.Item

这是一个循环进口


通过将
从django.contrib.auth.models import User
移动到Person模型内部,可以纠正这一问题。我不太明白为什么——如果有人想在我接受我自己的答案之前详细说明并提供更好的解释,请这样做。

如果没有看到您的代码,很难说。是否可能您意外地创建了一个名为
User
的变量,该变量正在对导入的同名类进行阴影处理?代码库非常大,肯定可以包含您希望看到的任何内容。这也是我的想法,但我什么也找不到。然而,什么会改变导入项目的能力呢?同样,如果没有看到代码,很难说。有一件事可能会影响它,那就是是否存在循环进口,你说你已经怀疑了。但是,人们无法看到您粘贴的小片段并神奇地说出问题可能在代码的其他地方;我们只能猜测。(这并不意味着你必须粘贴大量的代码,只是你可能需要做一些更详细的调试来缩小范围。)我绝对不会直接设置User=None。我有可能在全球范围内做了些什么吗?我明白了,你的头还在对着神秘的全球反弹到-
无问题。
 File "/Users/xxxxx/code/ownership/ownership/apps/Assets/models.py", line 12, in <module>
    import permissions   File "/Users/xxxxx/code/ownership/ownership/apps/Assets/permissions.py", line 4, in <module>
    from ownership.apps.Shared.models import Person   File "/Users/xxxxx/code/ownership/ownership/apps/Shared/models.py", line 87, in <module>
    from ownership.apps.Assets.models import Item