【发布时间】:2021-10-25 04:16:36
【问题描述】:
我的文件结构如下:
main.py
commands
| __init.py__
| a.py
| b.py
在我的__init__.py 中,我有以下代码可以从命令文件夹中动态导入所有内容:
from os.path import dirname, basename, isfile, join
import glob
modules = glob.glob(join(dirname(__file__), "*.py"))
__all__ = [ basename(f)[:-3] for f in modules if isfile(f) and not f.endswith('__init__.py')]
在main.py 中有from commands import *,
在a.py 和b.py 里面我有以下内容:
def foo():
return "bar"
我希望能够从变量中调用a 或b 中的foo(),有点像x = commands.{input()}.foo(),但我不知道该怎么做。
【问题讨论】: