Skip to main content
 首页 » 编程设计

python之Python 2.7 中的奇怪异常.SystemExit

2025年05月04日27EasonJim

这是我的代码和错误信息,有人知道为什么会出现这样的异常吗?谢谢。

源代码,

import sys 
import tensorflow as tf 
 
def main(argv): 
  print 'in main' 
def f(): 
    # this method will call def main(argv) 
    try: 
        tf.app.run() 
    except: 
        print "tf.app.run error ", sys.exc_info() 
 
if __name__ == "__main__": 
    f() 

错误代码,

in main 
tf.app.run error  (<type 'exceptions.SystemExit'>, SystemExit(), <traceback object at 0x10fa33f38>) 

请您参考如下方法:

这是预期的行为:tf.app.run()将 main() 的结果传递给 sys.exit() (以便更轻松地设置编辑代码),sys.exit() 引发 exceptions.SystemExit异常。

值得一提的是,使用 tf.app.run() 是完全可选的。许多 TensorFlow 脚本都包含它,因为它与 Google Python 编码风格更兼容。但是,如果您需要自定义脚本中的逻辑,您可以随意省略 tf.app.run()。 (这同样适用于 tf.app.flags。)