运行tf.app.run()时引发异常 [英] Exception thrown when running tf.app.run()

查看:143
本文介绍了运行tf.app.run()时引发异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此刻,我正在玩弄标志,并且在使用 tf.app.run()时遇到了一些奇怪的行为.以下代码段应仅打印通过命令行提供的字符串.

I am toying around with flags at the moment and came across some weird behavior when using tf.app.run(). The following code snippet should simply print the string given via the command line.

import tensorflow as tf

# command line flags
tf.app.flags.DEFINE_string('mystring', 'Hello World!',
                           '''String to print to console.''')

FLAGS = tf.app.flags.FLAGS


def main():

    print(FLAGS.mystring)

if __name__ == '__main__':
    tf.app.run()

在执行过程中,抛出此错误:

During execution, this error is thrown:

回溯(最近通话最近一次):

Traceback (most recent call last):

文件",第1行,在runfile('/path/flags.py',wdir ='/path')

File "", line 1, in runfile('/path/flags.py', wdir='/path')

文件"/home/abc/anaconda3/envs/tensorflow/lib/python3.5/site-packages/spyder/utils/site/sitecustomize.py",运行文件中的第710行execfile(文件名,命名空间)

File "/home/abc/anaconda3/envs/tensorflow/lib/python3.5/site-packages/spyder/utils/site/sitecustomize.py", line 710, in runfile execfile(filename, namespace)

文件"/home/abc/anaconda3/envs/tensorflow/lib/python3.5/site-packages/spyder/utils/site/sitecustomize.py",第101行,在execfile中exec(compile(f.read(),文件名,'exec'),命名空间)

File "/home/abc/anaconda3/envs/tensorflow/lib/python3.5/site-packages/spyder/utils/site/sitecustomize.py", line 101, in execfile exec(compile(f.read(), filename, 'exec'), namespace)

文件"/path/flags.py",第19行,在tf.app.run()

File "/path/flags.py", line 19, in tf.app.run()

文件"/home/abc/anaconda3/envs/tensorflow/lib/python3.5/site-packages/tensorflow/python/platform/app.py",126行,运行中_sys.exit(main(argv))

File "/home/abc/anaconda3/envs/tensorflow/lib/python3.5/site-packages/tensorflow/python/platform/app.py", line 126, in run _sys.exit(main(argv))

TypeError:main()接受0个位置参数,但给出了1个

TypeError: main() takes 0 positional arguments but 1 was given

...这很奇怪,因为我没有给main()一个参数.但是,如果我添加下划线 def main(_):,它可以正常工作而不会出现任何错误.

...which is strange because I do not give a single argument to main(). However, if I add an underscore def main(_):, it works without any errors.

我找不到描述下划线使用的文档.有人知道这里会发生什么吗?谢谢!

I couldn't find a doc where this is use of the underscore is described. Does anybody know what happens here? Thank you!

推荐答案

执行代码时在Pycharm IDE中看到的错误消息更加清晰.

The error message I see in Pycharm IDE when I execute your code is clearer.

Traceback (most recent call last):
  File "D:/PycharmProjects/TensorFlow/self.py", line 30, in <module>
    tf.app.run()
  File "D:\\Anaconda\envs\tensorflow\lib\site-packages\tensorflow\python\platform\app.py", 
line 48, in run
    _sys.exit(main(_sys.argv[:1] + flags_passthrough))
TypeError: main() takes 0 positional arguments but 1 was given

_sys.exit(main(_sys.argv [:1] + flags_passthrough))试图使用一个参数调用我们的 main 方法.

_sys.exit(main(_sys.argv[:1] + flags_passthrough)) is trying to call our main method with one argument.

这是run 方法"rel =" noreferrer> app.py

This is the run method in app.py

精简版的 run 方法可用于测试.

A stripped down version of the run method can be used to test.

import tensorflow as tf
import sys as _sys
from tensorflow.python.platform import flags


# command line flags
tf.app.flags.DEFINE_string('mystring', 'Hello World!',
                           '''String to print to console.''')

FLAGS = tf.app.flags.FLAGS

def run(main=None, argv=None):
  """Runs the program with an optional 'main' function and 'argv' list."""
  f = flags.FLAGS

  # Extract the args from the optional `argv` list.
  args = argv[1:] if argv else None

  # Parse the known flags from that list, or from the command
  # line otherwise.
  # pylint: disable=protected-access
  flags_passthrough = f._parse_flags(args=args)
  # pylint: enable=protected-access

  main = main or _sys.modules['__main__'].main

  print (_sys.argv[:1])

  # Call the main function, passing through any arguments
  # to the final program.
  #_sys.exit(main(_sys.argv[:1] + flags_passthrough))

  # Call the main function with no arguments
  #_sys.exit(main())


def main():
    print(FLAGS.mystring)

if __name__ == '__main__':
    #tf.app.run()
    run()

print(_sys.argv [1:])打印 ['D:/PycharmProjects/TensorFlow/self.py'] argv [0]是传递给解释器的脚本名称.

print(_sys.argv[1:]) prints ['D:/PycharmProjects/TensorFlow/self.py'] since argv[0] is the script name passed to the interpreter.

这篇关于运行tf.app.run()时引发异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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