Python,从模块导入函数

Lod*_*art 5 python import module

我有很多模块(数百个)。在每个模块中,我至少有一个功能。我的问题是如何只用一行从所有模块中导入所有函数?因为我不想这样做:

from tests.test_001 import test_001_func
from tests.test_002 import test_002_func
from tests.test_003 import test_003_func
...
from tests.test_999 import test_999_func
Run Code Online (Sandbox Code Playgroud)

test_xxx 是模块,这些模块包含 test_xxx_func 函数,所有模块都在一个文件夹中。

我想使用这样的东西:

from tests import *
test_001.test_001_func()
Run Code Online (Sandbox Code Playgroud)

但这不起作用

bgp*_*ter 0

tests您需要通过__all__在包文件中包含一个列表来提供包内容的索引__init__.py

在您的情况下,目录__init__.py中的文件tests将具有以下内容:

__all__ == ["test_001", "test_002" <<etc...>>]
Run Code Online (Sandbox Code Playgroud)

请参阅有关从包导入 *的文档。