在Jupyter Notebook中传递参数

2024-10-01 09:22:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我在读《艰难地学习Python》一书,现在在exc13。 具体做法如下:

from sys import argv
# read the WYSS section for how to run this
script, first, second, third = argv

print("The script is called:", script)
print("Your first variable is:", first)
print("Your second variable is:", second)
print("Your third variable is:", third)

但是当我运行这个程序时,我得到了以下错误

^{pr2}$

这是因为argv没有填充。书中说要使用一个终端,在终端中你可以通过键入以下内容来传递参数:

python ex13.py first 2nd 3rd

在终点站。但我怎么能只用Jupyter笔记本来做到这一点呢。在


Tags: fromimport终端readyourissysscript
1条回答
网友
1楼 · 发布于 2024-10-01 09:22:15

在Jupyter笔记本中,可以使用cell magic%%file创建一个文件。然后可以向shell发送一个命令,使用cell magic%%!来运行文件。在

要写出文件:

%%file ex13.py
from sys import argv
# read the WYSS section for how to run this
script, first, second, third = argv

print("The script is called:", script)
print("Your first variable is:", first)
print("Your second variable is:", second)
print("Your third variable is:", third)

要运行文件:

^{pr2}$

你应该看到你正在寻找的结果。打印输出被捕获并以列表形式返回,每个打印行一个元素。在

相关问题 更多 >