如何导入模块并运行它而不运行程序的其余部分?

2024-03-29 15:00:51 发布

您现在位置:Python中文网/ 问答频道 /正文

当我尝试导入一个模块以使用特定输入运行它时,我调用的python脚本的开始部分也会运行,它会要求我提供用户输入。我不允许以任何方式更改该文件的内容。我怎样才能抑制开始部分的运行,而只测试我想要单独测试的函数呢

module_path = os.path.join(root_dir, _dir, filename)  
mod = imp.load_source(modified_file, module_path)

point_counter = 0
if(mod.Function(2, 5) != True):
   point_counter+=1
if(mod.Function(2, 4) != False):
   point_counter+=1

Tags: 模块文件path函数用户脚本mod内容