python relative import weirdness

2019-06-02 08:39发布

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

标签: python import
1条回答
贼婆χ
2楼-- · 2019-06-02 08:56

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/
查看更多
登录 后发表回答