[英]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.