最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Some python file cant be imported as a module - Stack Overflow

programmeradmin5浏览0评论

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 the contents of model/__init__.py – John Gordon Commented Jan 18 at 5:04
  • @JohnGordon all __init__.py files are empty – Andika Eka Commented Jan 18 at 5:06
  • Shouldn't it contain some from . import role if you want role.py to be imported? – chrslg Commented Jan 18 at 8:41
  • I don't understand why dir(model) includes model_abstract, session, and user. There is no code here that would import those as part of model. Are you sure this is your exact code? – John Gordon Commented Jan 18 at 19:52
  • @chrslg you're right. I was an idiot. always thought __init__.py meant to be empty. sorry for wasting everyone's time – Andika Eka Commented Jan 19 at 3:42
 |  Show 1 more comment

2 Answers 2

Reset to default 0

I 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

发布评论

评论列表(0)

  1. 暂无评论