开发者

python relative import weirdness

开发者 https://www.devze.com 2023-03-08 18:51 出处:网络
I have a file: STARTDIR/module/submodule/config.py I have another file: STARDIR/utils/filesys/getAbsPath.py

I have a file:

STARTDIR/module/submodule/config.py

I have another file:

STARDIR/utils/filesys/getAbsPath.py

Why does this line work, in config.py?

from ..utils.filesys import getAbsPath

It seems like .. refers to module, not STARTDIR. There is no utils in module at all. In fact, doing

from .. import 开发者_运维知识库utils

yields

ImportError: cannot import name utils


This should work:

from ...utils.filesystem import getAbsPath

This is because:

  • from . import … imports from STARTDIR/module/submodule/
  • from .. import … imports from STARTDIR/module/
  • from ... import … imports from STARTDIR/
0

精彩评论

暂无评论...
验证码 换一张
取 消