Home > Back-end >  Python Django ImportError, cannot import name
Python Django ImportError, cannot import name

Time:05-03

I am having this issue with django project that has the following apps:

Accounts
Billing
Bookings
Notifications

The error appears as soon as I add the following to notifications/models.py:

import notifications.utils as U

The following is the full log of the exception:

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)

Any help on this will be greatly appreciated.

CodePudding user response:

There is a circular import here as you can see in the last half of the trace. Basically, adding the import notifications.utils as U creates a link in a chain that starts and ends at accounts\models.py

There are various ways of handling circular imports, but without the specifics of your code it's hard to see what would fit. This answer may have some useful suggestions.

  • Related