1、文件结构:
├── clazz
│ ├── __init__.py
│ ├── a.py
│ └── b.py
└── main.py
a.py
的代码
def show():
print("show A")
b.py
的代码
def show():
print("show B")
从main中导入clazz包中的a 和b 模块
main.py
import importlib
# 绝对导入
a = importlib.import_module("clazz.a")
a.show()
# 打印结果:show A
# 相对导入
b = importlib.import_module(".b", "clazz")
b.show()
# 打印结果:show B
注意,相对导入有个一点., 类似路径。
2.在一定文件下设定:
文件结构:
│──web_ui_auto_lib
│ ├── element_behavior_handlers
│ │ ├── __init__.py
│ │ ├── check_behavior_handler.py
│ │ └── click_behavior_handler.py
│ │ └── close_behavior_handler.py
│ │ └── f5_behavior_handler.py
│ └── main.py
main.py文件下:
# step.Behavior 是遍历步骤的名称:[check,click,close,f5]
module_name = "web_ui_auto_lib." + "element_behavior_handlers." + step.Behavior + "_behavior_handler"
# 每个py文件中的类名以 [check,click,close,f5] + BehaviorHandler
class_name = step.Behavior + "BehaviorHandler"
module = importlib.import_module(module_name)
# 对象可以引用 module_name 文件 + 类名,
# 所有check_behavior_handler.py,click_behavior_handler.py,close_behavior_handler.py,f5_behavior_handler.py文件下都是 handle_behavior 这个方法
aclass = getattr(module, class_name)
time.sleep(int(step.WaitTime))
handle_behavior = getattr(aclass, "handle_behavior")
returnvalue = handle_behavior(aclass(), self.bm, step)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)