Skip to main content
 首页 » 编程设计

python之来自 __future__ import absolute_import 不起作用子模块不可见

2025年05月04日66dudu

所以我在主作用域中有一个模块 bbb 以及 ccc。

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

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'] 

但是 bbb 和 ccc 似乎不可见。

我是不是漏掉了什么?

请您参考如下方法:

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