ipykernel_launcher.py:错误:需要以下参数:-m /-mode

时间:2020-09-25 23:14:49

标签: python argparse

我的代码很长,运行它时总是出现此错误。有人知道这个错误是什么吗?

`用法:ipykernel_launcher.py [-h] -m模式

ipykernel_launcher.py: error: the following arguments are required: -m/--mode

An exception has occurred, use %tb to see the full traceback.
 
SystemExit: 2`

1 个答案:

答案 0 :(得分:1)

ipython帮助中:

ipython [subcommand] [options] [-c cmd | -m mod | file] [--] [arg] ...

因此我们可以调用以下脚本来回显sys.argv

0812:~/mypy$ ipython echo.py -- foo bar
['/home/paul/mypy/echo.py', 'foo', 'bar']

或停留在交互式会话中:

0812:~/mypy$ ipython echo.py -- foo bar
['/home/paul/mypy/echo.py', 'foo', 'bar']
0824:~/mypy$ ipython -i echo.py -- foo bar
Python 3.6.9 (default, Jul 17 2020, 12:50:27) 
Type 'copyright', 'credits' or 'license' for more information
IPython 7.16.1 -- An enhanced Interactive Python. Type '?' for help.
['/home/paul/mypy/echo.py', 'foo', 'bar']

In [1]: sys.argv
Out[1]: ['/usr/local/bin/ipython', '-i', 'echo.py', '--', 'foo', 'bar']

sys.argv包含argparse解析器将处理的命令行字符串。只需提供期望的值(在usage中指定)即可。