parser.add_argument 和 parser.parse_args() 与 jupyter [英] parser.add_argument and parser.parse_args() with jupyter

查看:39
本文介绍了parser.add_argument 和 parser.parse_args() 与 jupyter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 jupyter notebook 运行一些代码,但从一开始,我就遇到了问题.

I'm trying to run some code with a jupyter notebook, but from the beginning, I have issues.

确实,看起来我不能使用这些命令:

Indeed, it looks like I can't use these commands:

parser.add_argument('--lr', default=0.1, type=float, help='learning rate')
parser.add_argument('--resume', '-r', action='store_true', help='resume from checkpoint')

用法:ipykernel_launcher.py [-h] [--lr LR] [--resume]ipykernel_launcher.py 给出错误:unrecognized arguments: -f

usage: ipykernel_launcher.py [-h] [--lr LR] [--resume] and ipykernel_launcher.py gives error: unrecognized arguments: -f

还有:

args = parser.parse_args()

/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/argparse.py in parse_args(self, args, namespace)
   1750         if argv:
   1751             msg = _('unrecognized arguments: %s')
-> 1752             self.error(msg % ' '.join(argv))
   1753         return args
   1754 

/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/argparse.py in error(self, message)
   2499         self.print_usage(_sys.stderr)
   2500         args = {'prog': self.prog, 'message': message}
-> 2501         self.exit(2, _('%(prog)s: error: %(message)s\n') % args)

/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/argparse.py in exit(self, status, message)
   2486         if message:
   2487             self._print_message(message, _sys.stderr)
-> 2488         _sys.exit(status)
   2489 
   2490     def error(self, message):

SystemExit: 2

我看到有些人在尝试使用 jupyter 和 arg_parse 时已经遇到了这个问题,但我找不到简单的解决方案.

I've seen that some people already had this problem when trying to use jupyter and arg_parse, but I can't find an easy solution.

感谢您的帮助!

推荐答案

当我启动一个空白笔记本并执行

When I launch a blank notebook and execute

import sys
sys.argv

我明白

['/usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py',
 '-f',
 '/run/user/1000/jupyter/kernel-b2d72478-6aaa-4206-9bf3-38fc1a0fd303.json']

parse_args() 使用 sys.argv 列表,该列表通常"来自调用您的脚本的 shell.它是一个命令行字符串列表.

parse_args() uses the sys.argv list that 'normally' comes from the shell that's calling your script. It is a list of the commandline strings.

您的脚本看到的 sys.argv 包含这个由 jupyter 创建的 -f 参数.

The sys.argv that your script sees includes this -f argument, created by jupyter.

您可以使用 parse_known_args() 以便您的解析器不会引发此错误.但是你不会帮助你获得 --lr-r 参数 - 因为 jupyter's 自己的解析器会拒绝它们.

You could use parse_known_args() so that your parser doesn't raise this error. But you that won't help you get --lr or -r arguments - because jupyter's own parser will reject them.

之前的 SO 问题的本质是您不能向笔记本提供命令行参数.

The essence of the previous SO questions is that you can't provide commandline arguments to a notebooks.

这篇关于parser.add_argument 和 parser.parse_args() 与 jupyter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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