from __future__ import absolute_import 不起作用?子模块不可见

from __future__ import absolute_import not working? sub-modules not visible

所以我在主范围内有一个模块 bbb 以及 ccc

我正在添加一个名为 tools 的库,它还有两个名为 bbbccc 的模块:

tools

  • __init__.py
  • aaa.py
  • bbb.py
  • ccc.py

bbb.py 中,我正在导入主作用域 bbb:

from __future__ import absolute_import
import bbb

并在 ccc.py 中做同样的事情:

from __future__ import absolute_import
import ccc

但是当我导入工具并对其进行 dir 时,我只能看到:

['__builtins__', '__doc__', '__file__',
'__name__', '__package__', '__path__', 'aaa']

但是 bbbccc 似乎不可见。

我是不是漏掉了什么?

使用点符号:

来自bbb.py,如果要导入aaa.py

from . import aaa

如果要从外部工具导入 tools/aaa.py:

from tools import aaa

but when I import tools and dir it I can only see:

['__builtins__', '__doc__', '__file__',
'__name__', '__package__', '__path__', 'aaa']

but the bbb and ccc don't seem to be visible.

导入包不会自动加载它的所有子模块。如果你想使用tools.bbb包,你需要做

import tools.bbb
# or
from tools import bbb

import tools 不会剪的。或者,您可以让 tools 在其 __init__.py:

中显式加载其子模块
# in __init__.py
from . import aaa, bbb, ccc