In a Django project, I need to import class-A from file-1 in file-2. and import class-B from file-2 in file-1.
These classes have their unique methods, and I want to use these methods within another file (like description above).
I'm working with:
Python 3.8.10 (within a virtual environment)
Windows 10 - 64 bit (latest build)
Django 4.0.4
When I runpython manage.py runserver
, I see errors below:
(my_ea_proj) PS F:\my_ea_proj\ea_proj> python.exe .\manage.py runserver
Traceback (most recent call last):
File ".\manage.py", line 30, in <module>
main()
File ".\manage.py", line 13, in main
django.setup()
File "F:\my_ea_proj\lib\site-packages\django\__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "F:\my_ea_proj\lib\site-packages\django\apps\registry.py", line 116, in populate
app_config.import_models()
File "F:\my_ea_proj\lib\site-packages\django\apps\config.py", line 304, in import_models
self.models_module = import_module(models_module_name)
File "C:\Python38\lib\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 "F:\my_ea_proj\ea_proj\ea_app\models.py", line 16, in <module>
from .login_request_based import full_request
File "F:\my_ea_proj\ea_proj\ea_app\login_request_based.py", line 10, in <module>
from ea_app.all_utils.ea_utils import ea_utils_class
File "F:\my_ea_proj\ea_proj\ea_app\all_utils\ea_utils.py", line 10, in <module>
from ea_app.login_request_based import full_request
ImportError: cannot import name 'full_request' from partially initialized module 'ea_app.login_request_based' (most likely due to a circular import) (F:\my_ea_proj\ea_proj\ea_app\login_request_based.py)
F:\my_ea_proj\ea_proj>tree /F
F:.
│ manage.py
│ __init__.py
│
├───ea_app
│ │ admin.py
│ │ apps.py
│ │ login_request_based.py
│ │ models.py
│ │ tasks.py
│ │ tests.py
│ │ urls.py
│ │ views.py
│ │ __init__.py
│ │
│ ├───all_utils
│ │ ea_remote_db.py
│ │ ea_utils.py
│ │ __init__.py
│ │
│ ├───migrations
│ │ __init__.py
│ | ...skipped other migration files...
│
└───ea_proj
asgi.py
settings.py
urls.py
wsgi.py
__init__.py
When I add below classes in ea_utils.py
and login_request_based.py
files, I got error ImportError: cannot import name 'full_request' from partially initialized module 'ea_app.login_request_based' (most likely due to a circular import)...
. But when I comment-out one of them, problem fixed and built-in web server is launched successfully.
F:\my_ea_proj\ea_proj\ea_app\all_utils\ea_utils.py:
from ea_app.login_request_based import full_request
F:\my_ea_proj\ea_proj\ea_app\login_request_based.py
from ea_app.all_utils.ea_utils import ea_utils_class
How to fix this issue?.
Thank you @mohamed-beltagy. but your comment/link didn't fix my problem. Please read note below.
What happens when using mutual or circular (cyclic) imports in Python?
If you do import foo (inside bar.py) and import bar (inside foo.py), it will work fine. By the time anything actually runs, both modules will be fully loaded and will have references to each other.
The problem is when instead you do from foo import abc (inside bar.py) and from bar import xyz (inside foo.py). Because now each module requires the other module to already be imported (so that the name we are importing exists) before it can be imported.
Note, if you do from foo import * and from bar import *, everything executed in the foo is in the initializing phase of bar, and the actual functions in bar has not yet been defined...