开发者

python relative import weirdness

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜