python导入指定路径下的包


import importlib.util

def import_module_by_path(module_path,module_name):
    """
    根据给定的完整路径动态导入模块
    """
    spec = importlib.util.spec_from_file_location(module_name, module_path)
    module = importlib.util.module_from_spec(spec)
    spec.loader.exec_module(module)
    return module

# 使用动态导入的模块
module_path = r"yourModule\libA.py"
moduleName = 'libA'
module = import_module_by_path(module_path, moduleName)
myclass = getattr(module,moduleName)
tempInstance = myclass()
tempInstance.greet("hello world")

 

posted @ 2024-03-10 20:26  朵朵奇fa  阅读(181)  评论(0编辑  收藏  举报