如何从python中的上层脚本扫描脚本以获取返回值? [英] How do scan a script for return values from a upper script in python?

查看:64
本文介绍了如何从python中的上层脚本扫描脚本以获取返回值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

import os
import pdb

os.system("ToBuildOrNot.py MSS_sims")

for output in os.system:
     if ToBuildOrNot is True:
         print "The MSS_sims Needs To rebuilt"

     elif ToBuildOrNot is False:
         print "The MSS_sism does NOT Need to be Rebuilt"

     else:
         print "error"

推荐答案

不要使用系统从 Python 脚本调用 Python 脚本,这会产生一个完整的其他解释器.只需导入它.像这样:

Don't invoke a Python script from a Python script by using system, which spawns a whole other interpreter. Just import it. Like this:

import ToBuildOrNot
needsBuild = ToBuildOrNot.run() # or whatever you call your top-level function

由于 ToBuildOrNot.py 现在是一个脚本,请确保main"函数受到保护,因此它不会在导入时自动执行.大多数人在 Python 中都是这样做的:如果 __name__ == "__main__" 会怎样:做什么?

Since ToBuildOrNot.py is a script now, make sure the "main" function is protected so it doesn't execute automatically on import. Most people do it this way in Python: What does if __name__ == "__main__": do?

这篇关于如何从python中的上层脚本扫描脚本以获取返回值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆