-2

I read about solution for the error (write import instead of from ...) but it doesn't work I think because I have a complex folder structure.

enter image description here

quiz.models

import apps.courses.models as courses_models
courses_models.Lesson  # some actions with that class


class Quiz:
    pass

courses.models

import apps.quiz.models as quiz_models
quiz_models.Quiz  # some actions with that class


class Lesson:
    pass

Error:

Traceback (most recent call last):
  File "manage.py", line 21, in <module>
    main()
  File "manage.py", line 17, in main
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python3.8/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python3.8/site-packages/django/core/management/__init__.py", line 377, in execute
    django.setup()
  File "/usr/local/lib/python3.8/site-packages/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/usr/local/lib/python3.8/site-packages/django/apps/registry.py", line 114, in populate
    app_config.import_models()
  File "/usr/local/lib/python3.8/site-packages/django/apps/config.py", line 211, in import_models
    self.models_module = import_module(models_module_name)
  File "/usr/local/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 848, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/code/apps/carts/models.py", line 5, in <module>
    from ..courses.models import Course
  File "/code/apps/courses/models.py", line 12, in <module>
    import apps.quiz.models as quiz_models
  File "/code/apps/quiz/models.py", line 12, in <module>
    class Quiz(TimestampedModel):
  File "/code/apps/quiz/models.py", line 15, in Quiz
    courses_models.Lesson, default=1, on_delete=models.DO_NOTHING, verbose_name=_('Lesson'), related_name='quizzes')
AttributeError: partially initialized module 'apps.courses.models' has no attribute 'Lesson' (most likely due to a circular import)

Maybe you have other solutions for circular importing or I just wrote the imports incorrectly?

Hahan't
  • 481
  • 1
  • 4
  • 11
  • Does this answer your question? [Circular import dependency in Python](https://stackoverflow.com/questions/1556387/circular-import-dependency-in-python) – ThatXliner May 07 '21 at 23:36
  • If module A imports module B, then B, meaning all of the code in it, gets executed when A imports it. If code in B refers to an object in A, that hasn't been defined yet because execution of A is still at the `import B` statement in line 1, but the definition of that object is at line 322 of A, then you will get a circular import failure. There is no way you can change the way you write the `import` statements to fix it. Either combine the 2 modules; or (if possible) postpone `import B` until after A defines the object that B refers to; or move the mutually dependent code to its own module. – BoarGules May 07 '21 at 23:47
  • if you know Django check that please, there's my detail problem https://stackoverflow.com/questions/67446192/django-models-how-to-fix-circular-import-error – Hahan't May 08 '21 at 09:52

1 Answers1

0

Yes, this is a circular import. course.models is importing quiz.models while quiz.models is also importing course.models.

To avoid this, you could defer the import until you really need it.

ThatXliner
  • 414
  • 5
  • 15
  • if you know Django check that please, there's my detail problem https://stackoverflow.com/questions/67446192/django-models-how-to-fix-circular-import-error – Hahan't May 08 '21 at 09:51