Python Django 导入错误,无法导入名称

Python Django ImportError, cannot import name

我在使用具有以下应用程序的 django 项目时遇到了这个问题:

Accounts
Billing
Bookings
Notifications

将以下内容添加到 notifications/models.py:

后立即出现错误
import notifications.utils as U

以下是异常的完整日志:

D:\Noki\code\nokidepot_tcp\website\noki_proj>python manage.py runserver 192.168.1.103:8000
Watching for file changes with StatReloader
Exception in thread django-main-thread:
Traceback (most recent call last):
  File "C:\Users\jahanzeb\AppData\Local\Programs\Python\Python37-32\lib\threading.py", line 926, in _bootstrap_inner
    self.run()
  File "C:\Users\jahanzeb\AppData\Local\Programs\Python\Python37-32\lib\threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "D:\Noki\code\nokidepot_tcp\nokienv\lib\site-packages\django\utils\autoreload.py", line 54, in wrapper
    fn(*args, **kwargs)
  File "D:\Noki\code\nokidepot_tcp\nokienv\lib\site-packages\django\core\management\commands\runserver.py", line 109, in inner_run
    autoreload.raise_last_exception()
  File "D:\Noki\code\nokidepot_tcp\nokienv\lib\site-packages\django\utils\autoreload.py", line 77, in raise_last_exception
    raise _exception[1]
  File "D:\Noki\code\nokidepot_tcp\nokienv\lib\site-packages\django\core\management\__init__.py", line 337, in execute
    autoreload.check_errors(django.setup)()
  File "D:\Noki\code\nokidepot_tcp\nokienv\lib\site-packages\django\utils\autoreload.py", line 54, in wrapper
    fn(*args, **kwargs)
  File "D:\Noki\code\nokidepot_tcp\nokienv\lib\site-packages\django\__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "D:\Noki\code\nokidepot_tcp\nokienv\lib\site-packages\django\apps\registry.py", line 114, in populate
    app_config.import_models()
  File "D:\Noki\code\nokidepot_tcp\nokienv\lib\site-packages\django\apps\config.py", line 211, in import_models
    self.models_module = import_module(models_module_name)
  File "C:\Users\jahanzeb\AppData\Local\Programs\Python\Python37-32\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 728, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "D:\Noki\code\nokidepot_tcp\website\noki_proj\accounts\models.py", line 13, in <module>
    from billing.views import charge_booking
  File "D:\Noki\code\nokidepot_tcp\website\noki_proj\billing\views.py", line 14, in <module>
    from bookings.models import *
  File "D:\Noki\code\nokidepot_tcp\website\noki_proj\bookings\models.py", line 9, in <module>
    from notifications.views import create_event
  File "D:\Noki\code\nokidepot_tcp\website\noki_proj\notifications\views.py", line 3, in <module>
    from .models import Events, get_event_string, get_event_time
  File "D:\Noki\code\nokidepot_tcp\website\noki_proj\notifications\models.py", line 8, in <module>
    import notifications.utils as U
  File "D:\Noki\code\nokidepot_tcp\website\noki_proj\notifications\utils.py", line 1, in <module>
    from accounts.views import get_user_by_phone
  File "D:\Noki\code\nokidepot_tcp\website\noki_proj\accounts\views.py", line 7, in <module>
    from .models import NokiUser, EmailActivationLinks
ImportError: cannot import name 'NokiUser' from 'accounts.models' (D:\Noki\code\nokidepot_tcp\website\noki_proj\accounts\models.py)

如有任何帮助,我们将不胜感激。

如您在跟踪的后半部分中所见,此处存在循环导入。基本上,添加导入 notifications.utils 作为 U 在开始和结束于 accounts\models.py

的链中创建一个 link

有多种处理循环导入的方法,但如果没有您的代码的细节,很难看出哪种方法适合。 This answer可能会有一些有用的建议。