Python 如何修复 ipykernel_launcher.py: error: unrecognized arguments in jupyter?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48796169/
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 18:51:45  来源:igfitidea点击:

How to fix ipykernel_launcher.py: error: unrecognized arguments in jupyter?

pythonpython-3.xtensorflowjupyter-notebookjupyter

提问by virtualdvid

I am following this tensorflow tutorialafter two days setting up the environment I finally could run premade_estimator.pyusing cmd

两天后,我正在关注这个 tensorflow教程,我终于可以premade_estimator.py使用 cmd运行环境了

but when I try to run the same code in a jupyter notebook I am getting this error:

但是当我尝试在 jupyter notebook 中运行相同的代码时,我收到此错误:

usage: ipykernel_launcher.py [-h] [--batch_size BATCH_SIZE]
                             [--train_steps TRAIN_STEPS]

ipykernel_launcher.py: error: unrecognized arguments: -f C:\Users\david\AppData\Roaming\jupyter\runtime\kernel-4faecb24-6e87-40b4-bf15-5d24520d7130.json

An exception has occurred, use %tb to see the full traceback.

SystemExit: 2

C:\Anaconda3\envs\python3x\lib\site-packages\IPython\core\interactiveshell.py:2918: 
UserWarning: To exit: use 'exit', 'quit', or Ctrl-D. warn("To exit: use 'exit', 'quit', or Ctrl-D.", stacklevel=1)
usage: ipykernel_launcher.py [-h] [--batch_size BATCH_SIZE]
                             [--train_steps TRAIN_STEPS]

ipykernel_launcher.py: error: unrecognized arguments: -f C:\Users\david\AppData\Roaming\jupyter\runtime\kernel-4faecb24-6e87-40b4-bf15-5d24520d7130.json

发生异常,请使用 %tb 查看完整的回溯。

SystemExit: 2

C:\Anaconda3\envs\python3x\lib\site-packages\IPython\core\interactiveshell.py:2918: 
UserWarning: To exit: use 'exit', 'quit', or Ctrl-D. warn("To exit: use 'exit', 'quit', or Ctrl-D.", stacklevel=1)

I have tried to fix it without success using:

我尝试使用以下方法修复它但没有成功:

pip install --ignore-installed --upgrade jupyter

pip install ipykernel
python -m ipykernel install

conda install notebook ipykernel
ipython kernelspec install-self

Any idea will be appreciate! Thanks!

任何想法将不胜感激!谢谢!

回答by virtualdvid

I got it! the reason why we get the error is because this code is using argparseand this module is used to write user-friendly command-line interfaces, so it seems, it has a conflict with Jupyter Notebook.

我知道了!之所以会报错,是因为这段代码正在使用,argparse并且这个模块是用来编写用户友好的命令行界面的,所以看起来,它和 Jupyter Notebook 有冲突。

I found the solution in this page:

我在这个页面找到了解决方案:

What we have to do is:

我们要做的是:

Delete or comment these lines:

删除或注释这些行:

parser = argparse.ArgumentParser()
parser.add_argument('--batch_size', default=100, type=int, help='batch size')
parser.add_argument('--train_steps', default=1000, type=int,
                    help='number of training steps')

and replace args

并替换 args

args = parser.parse_args(argv[1:])

for a dictionary using the library easydictin this way:

对于easydict以这种方式使用库的字典:

args = easydict.EasyDict({
    "batch_size": 100,
    "train_steps": 1000
})

With easydictwe can access dict values as attributes for the arguments.

随着easydict我们可以访问字典的值作为参数属性。

Update

更新

After all this year diving deeper in python, I found the solution for this question is way more simple (We don't need to use any external library or method). argparseis just one of the many ways to pass arguments to a script in python from the terminal. When I tried to do it in a jupyter notebook obviously that wasn't going to work. We can just replace in the function directly the parameters like:

在今年深入研究 python 之后,我发现这个问题的解决方案更简单(我们不需要使用任何外部库或方法)。argparse只是从终端将参数传递给 python 脚本的众多方法之一。当我尝试在 jupyter 笔记本中执行此操作时,这显然行不通。我们可以直接在函数中替换参数,例如:

funtion(batch_size=100, train_steps=1000)

Now, if we have a long list of parameters for our function, we can use *argsor **kargs.

现在,如果我们的函数有很长的参数列表,我们可以使用*args**kargs

*argspass a tuple as parameters in the function, for this case, in particular, it will be:

*args在函数中传递一个元组作为参数,特别是在这种情况下,它将是:

args = (100, 1000)
function(*args)

**kargspass a dictionary as arguments to our function:

**kargs将字典作为参数传递给我们的函数:

args = {"batch_size": 100,
        "train_steps": 1000}
function(**args)

just google it and you will find a really good explanation on how to use them both, here one documentationthat I used to study this.

只需谷歌一下,你就会找到一个关于如何使用它们的非常好的解释,这里有一个我用来研究这个的文档

回答by Badger Titan

A more elegant solution would be:

一个更优雅的解决方案是:

args, unknown = parser.parse_known_args()

instead of

代替

args = parser.parse_args()

回答by pauljohn32

I just ran into this problem today and found a quick, stupid solution is to insert an argument processor for the -fargument that qtconsole/ipython passes though and we did not expect. At end of parser.add_argumentI put in:

我今天刚遇到这个问题,发现一个快速、愚蠢的解决方案是为-fqtconsole/ipython 通过但我们没想到的参数插入一个参数处理器。最后parser.add_argument我输入:

parser.add_argument("-f", "--fff", help="a dummy argument to fool ipython", default="1")

I don't use the -fparameter, so there's no loss for me.

我不使用-f参数,所以我没有损失。

I'd rather not re-engineer a larger argument processing framework just because of ipython cropping up in workflow on one particular computer...

我宁愿不重新设计一个更大的参数处理框架,因为 ipython 在一台特定的计算机上的工作流中突然出现......

回答by An0n

Have you tried :

你有没有尝试过 :

conda install ipykernel --name Python3
python -m ipykernel install