【发布时间】:2018-05-15 22:28:58
【问题描述】:
我正在尝试在 jupyter notebook 中运行以下代码。
import argparse
parser = argparse.ArgumentParser(description='Example with non-optional arguments')
parser.add_argument('count', action="store", type=int)
parser.add_argument('units', action="store")
print(parser.parse_args())
但这给出了以下错误
usage: ipykernel_launcher.py [-h] count units
ipykernel_launcher.py: error: argument count: invalid int value: 'C:\\Users\\Kwan Lee\\AppData\\Roaming\\jupyter\\runtime\\kernel-76bf5bb5-ea74-42d5-8164-5c56b75bfafc.json'
An exception has occurred, use %tb to see the full traceback.
SystemExit: 2
c:\users\kwan lee\anaconda2\envs\tensorflow\lib\site-packages\IPython\core\interactiveshell.py:2971: UserWarning: To exit: use 'exit', 'quit', or Ctrl-D.
warn("To exit: use 'exit', 'quit', or Ctrl-D.", stacklevel=1)
我只是想了解 argparse 是什么,但我没有收到此错误。
【问题讨论】:
-
来自
notebook,您需要将其称为parser.parse_args(['5', 'inches'])。也就是说,使用模拟的字符串列表。sys.argv使用的运行笔记本将不适合此脚本。