[英]Pass input parameters to script from jupyter notebook
我正在尝试运行由 Jupyter 笔记本中的文件提供的 python 脚本。 如果我使用以下命令,脚本正在运行:
!python script.py --input_path /folder/input --output_path /folder/output/
但是我需要从笔记本的变量中传递这些路径。 我该怎么做?
试过这个,但没有用:
input = "/folder/input"
output = "/folder/output/"
!python script.py --input_path input --output_path output
可以使用{}
表示法将值传递给 shell 命令。 有关 Python 运行时和 shell 命令之间的其他处理,请参阅此链接。
例如:
input = "/folder/input"
output = "/folder/output/"
!echo python script.py --input_path "{input}" --output_path "{output}"
打印以下输出:
python script.py --input_path "/folder/input" --output_path "/folder/output/"
显然,可以删除echo
以使其实际调用python
命令,这就是原始问题所追求的。
使用 subprocess 包可以吗? 在这里,我在与笔记本相同的文件夹中运行脚本。 它运作良好。
import subprocess
input_file = '/folder/input'
output_file = '/folder/output'
subprocess.run('python script.py --input_path {} --output_path {}'.format(input_file, output_file))
使用$
符号访问 Python 变量。
input = "/folder/input"
output = "/folder/output/"
!python script.py --input_path $input --output_path $output
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.