我的代码很长,运行它时总是出现此错误。有人知道这个错误是什么吗?
`用法: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`
答案 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
中指定)即可。