Python: 从兄弟目录导入文件,父目录同名

Python: import file from sibling directory, where parent directory has the same name

我想从同级包的另一个模块内的包中的模块导入对象。但是父目录与其中一个包同名。

这是我正在苦苦挣扎的结构:

foo{
  __init__.py
  foo{
    __init__.py
    models.py
  }
  bar{
    __init__.py
    models.py
  }
}

foo 内部(不是父项)我想从 bar.models.

导入一个对象

我试过这个:

from foo.bar.models import MyObject

from ..bar.models import MyObject

None工作了,你好吗?

要从另一个兄弟目录导入,使用 sys 模块和 path:

效率更高
import sys
sys.path.append('../bar')
import models
# ...