Python AttributeError: 'module' 对象没有 tf.app.run() 的属性 'main'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43004243/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 22:24:41  来源:igfitidea点击:

AttributeError: 'module' object has no attribute 'main' for tf.app.run()

pythontensorflow

提问by user288609

I am trying to test a short program, which is pretty simple, shown as follows

我正在尝试测试一个简短的程序,它非常简单,如下所示

import numpy as np
import tensorflow as tf    
flags = tf.app.flags
FLAGS = flags.FLAGS    
import tensorvision.train as train
import tensorvision.utils as utils    
flags.DEFINE_string('name', None,
                    'Append a name Tag to run.')

flags.DEFINE_string('hypes', 'hypes/medseg.json',
                    'File storing model parameters.')    
if __name__ == '__main__':
    tf.app.run()

However, running the program gives the following error message,

但是,运行该程序会出现以下错误消息,

Traceback (most recent call last):
File "train.py", line 43, in <module>
  tf.app.run()
File "/devl/tensorflow/tf_0.12/lib/python3.4/site-  packages/tensorflow/python/platform/app.py", line 39, in run
main = main or sys.modules['__main__'].main
AttributeError: 'module' object has no attribute 'main'

回答by John Ching

You either need a "def main(args)" function in your file:

您的文件中需要一个“def main(args)”函数:

import numpy as np
import tensorflow as tf    
flags = tf.app.flags
FLAGS = flags.FLAGS    
import tensorvision.train as train
import tensorvision.utils as utils    

def main(args):
    flags.DEFINE_string('name', None,
                        'Append a name Tag to run.')

    flags.DEFINE_string('hypes', 'hypes/medseg.json',
                        'File storing model parameters.')    
if __name__ == '__main__':
    tf.app.run()

or tf.app.run() can call an external function

或者 tf.app.run() 可以调用外部函数

tf.app.run(my_func)