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 fromSTARTDIR/module/submodule/
from .. import …
imports fromSTARTDIR/module/
from ... import …
imports fromSTARTDIR/
精彩评论