argparse library: PythonShell.py: error: unrecognized arguments

argparse library: PythonShell.py: error: unrecognized arguments

我在 databricks 中名为“样本”的笔记本中有下面这段代码

import argparse

parser = argparse.ArgumentParser()
parser.add_argument('--arg1')
parser.add_argument('--arg2')

arguments = parser.parse_args()

print(arguments)

然后我尝试 运行 来自不同笔记本的“样本”笔记本,就像这样

%run ./sample $arg1="some_text" $arg2="some_text"

我收到以下错误:

usage: PythonShell.py [-h] [--arg1 ARG1] [--arg2 ARG2]
PythonShell.py: error: unrecognized arguments: 35415 0 50000 1000 620e09fb254e45a38e2499618d76c4b8 3.0.1 f580cca74ed613c678816aa76dd5a75912dbd0bb81fe8bb5d2e7a7434ff5998a unpinned
The execution of this command did not finish successfully
Command took 0.16 seconds -- by 7/12/2021, 7:40:13 PM
usage: PythonShell.py [-h] [--arg1 ARG1] [--arg2 ARG2]
PythonShell.py: error: unrecognized arguments: 35415 0 50000 1000 620e09fb254e45a38e2499618d76c4b8 3.0.1 f580cca74ed613c678816aa76dd5a75912dbd0bb81fe8bb5d2e7a7434ff5998a unpinned

你应该用这个 %run ./sample --arg1="some_text" --arg2="some_text"

如果您使用 argparse.ArgumentParser,那么它将检索附加到您的笔记本的 Python 解释器的参数,而不是您传递给 sample 笔记本的参数.

如果要检索传递给 %rundbutils.notebook.run function, then you need to use the dbutils.widgets.get function 的参数,该参数接收一个参数 - 参数名称。例如,如果您将其放入 sample 笔记本中:

print("arg1=", dbutils.widgets.get("arg1"))
print("arg2=", dbutils.widgets.get("arg2"))

你会看到结果:

arg1= some_text1
arg2= some_text2