我想使用 __init__.py 将包中的所有模块作为别名导入 __main__ 中,以便可以简单地从交互模式调用它们。例如,这是一个示例文件树:
foobar/
__init__.py
foo.py
bar.py
从python解释器我希望能够导入包并使用定义的别名访问所有模块,如下所示:
>>> import foobar
>>> <module 'foobar' from 'C:\...'>
>>> f.func()
>>> b.func()
这将要求 __init__.py 包含以下内容:
# __init__.py
from . import foo as f
from . import bar as b
# these will not work
__main__.f = f
__main__.b = b
我怎样才能使这项工作?
编辑
我不想使用from foobar import *
,因为它不允许我使用别名。
from foobar import foo as f
每次我启动交互模式时,为每个模块都打字效率不高,因为可能有数百个模块。