python通过import导入带“-”文件名称的模块

在python里,由于连接符不能作为模块的名称,是不能导入的。如果导入,会出错如下:这里是想导入 exmple-plugin.py模块。 其实可以使用__import__内置函数来实现,如下:
# File: builtin-import-example-1.py

import glob, os

modules = []

for module_file in glob.glob("*-plugin.py"):
    try:
        module_name, ext = os.path.splitext(os.path.basename(module_file))
        module = __import__(module_name)
        modules.append(module)
    except ImportError:
        pass # ignore broken modules

# say hello to all modules
for module in modules:
    module.hello()
而example-plugin.py文件的内容如下:
# File: example-plugin.py

def hello():
    print("example-plugin says hello")
在这使用 module = __import__(module_name)