I have to import a directory as a module in python. I can't import file individually to avoid circular import. howerver only some of the file is available in the module.
here is my dir structure :
./database
├── connection.py
├── exception.py
├── __init__.py
├── model
│ ├── ddd.py
│ ├── __init__.py
│ ├── model_abstract.py
│ ├── __pycache__
│ │ ├── __init__.cpython-313.pyc
│ │ ├── model_abstract.cpython-313.pyc
│ │ ├── model.cpython-313.pyc
│ │ ├── role.cpython-313.pyc
│ │ ├── session.cpython-313.pyc
│ │ └── user.cpython-313.pyc
│ ├── role.py
│ ├── session.py
│ └── user.py
└── __pycache__
├── connection.cpython-313.pyc
├── exception.cpython-313.pyc
└── __init__.cpython-313.pyc
4 directories, 18 files
when I'm doing import from user.py file
from database import model
print(dir(model))
x = model.role.Role.get_by_id(1)
i got this error:
AttributeError: module 'database.model' has no attribute 'role'
from database import model
print(dir(model))
role
is not one of the attributes while model_abstract
, session
, user
are
['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', 'model_abstract', 'session', 'user']
this code works fine
model.session.UserSession.get_by_user_id(1)
What is happening?
ps. role.py manually imported somewhere else. works fine on that file but not on this.
I have to import a directory as a module in python. I can't import file individually to avoid circular import. howerver only some of the file is available in the module.
here is my dir structure :
./database
├── connection.py
├── exception.py
├── __init__.py
├── model
│ ├── ddd.py
│ ├── __init__.py
│ ├── model_abstract.py
│ ├── __pycache__
│ │ ├── __init__.cpython-313.pyc
│ │ ├── model_abstract.cpython-313.pyc
│ │ ├── model.cpython-313.pyc
│ │ ├── role.cpython-313.pyc
│ │ ├── session.cpython-313.pyc
│ │ └── user.cpython-313.pyc
│ ├── role.py
│ ├── session.py
│ └── user.py
└── __pycache__
├── connection.cpython-313.pyc
├── exception.cpython-313.pyc
└── __init__.cpython-313.pyc
4 directories, 18 files
when I'm doing import from user.py file
from database import model
print(dir(model))
x = model.role.Role.get_by_id(1)
i got this error:
AttributeError: module 'database.model' has no attribute 'role'
from database import model
print(dir(model))
role
is not one of the attributes while model_abstract
, session
, user
are
['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', 'model_abstract', 'session', 'user']
this code works fine
model.session.UserSession.get_by_user_id(1)
What is happening?
ps. role.py manually imported somewhere else. works fine on that file but not on this.
Share Improve this question asked Jan 18 at 4:35 Andika EkaAndika Eka 391 silver badge9 bronze badges 6 | Show 1 more comment2 Answers
Reset to default 0I was being dumb. as @JohnGordon an @chrslg point out, importing role.py
in __init__.py
solve the problem:
from . import role
I think you should consider resolving circular imports by moving the imports inside functions or methods, or using forward declarations if the imports are needed only for type checking. However, the simplest solution for most cases is to refactor the code structure to eliminate the circular dependency. (Refer to What happens when using mutual or circular (cyclic) imports? for more information)
The following is a a simple solution to the issue you faced with importing:
from .role import Role
x = Role.get_by_id(1)
another situation that could occur is that the specific Role
is not exposed in the module namespace, so you would need to fix as follows:
./database/model/__init__.py
from .role import Role
__all__ = ["Role"]
Hope this helps
model/__init__.py
– John Gordon Commented Jan 18 at 5:04__init__.py
files are empty – Andika Eka Commented Jan 18 at 5:06from . import role
if you wantrole.py
to be imported? – chrslg Commented Jan 18 at 8:41dir(model)
includesmodel_abstract
,session
, anduser
. There is no code here that would import those as part ofmodel
. Are you sure this is your exact code? – John Gordon Commented Jan 18 at 19:52__init__.py
meant to be empty. sorry for wasting everyone's time – Andika Eka Commented Jan 19 at 3:42