文件结构:
─test_pack
│ func.py
│ test.py
func.py
class test_classname():
def test_class_fun(self,info):
print(info)
def test_func(info):
print(info)
test.py
import importlib
module_path = '.func'
class_name = 'test_classname'
class_func_name = 'test_class_fun'
func_name = 'test_func'
obj = importlib.import_module(module_path,package='test_pack') # 导入该文件
test_class = getattr(obj,class_name) # 获取该文件内指定类
test_class_func = getattr(test_class(),class_func_name) #获取类的某个函数
test_class_func('测试1') #运行函数
test_func = getattr(obj,func_name) # 获取该文件内指定函数
test_func('测试2') # 运行该函数
运行结果:
测试1
测试2
参考文档:https://www.cnblogs.com/bluefrog/archive/2012/05/11/2496439.html
https://www.freesion.com/article/42691046180/