首页 > 解决方案 > 导入多个模块中的所有功能

问题描述

我想在多个模块中导入所有功能。

这是我的结构:

global/
  __init__.py
  test/
    __init__.py
    a.py
    b.py

我建立 :

test/__init__.py

from .a import *
from .b import * 

和,

global/__init__.py
from .test import *

我可以从所有模块调用 alls 函数。但是,我希望只有一行直接导入所有模块中的所有函数,而不是逐个模块调用。

标签: pythonpython-import

解决方案


这是你要找的吗?

app.py

from test.a import *
from test.b import *

foo()
bar()

您现在可以在a.pyb.py模块中指定尽可能多的函数,而无需实际调用前缀模块。

test/a.py

def foo():
    print('foo')

test/b.py

def bar():
    print('bar')

输出:

foo
bar

推荐阅读