I am trying to run a python script provided by a file from a Jupyter notebook. The script is running if I use the following command:
JavaScript
x
2
1
!python script.py --input_path /folder/input --output_path /folder/output/
2
But I need to pass those paths from a variable of my notebook. How can I do it?
Tried this but it didn’t work:
JavaScript
1
5
1
input = "/folder/input"
2
output = "/folder/output/"
3
4
!python script.py --input_path input --output_path output
5
Advertisement
Answer
Values can be passed through to the shell command using the {}
notation. See this link for additional handling between the Python runtime and shell commands.
For example:
JavaScript
1
5
1
input = "/folder/input"
2
output = "/folder/output/"
3
4
!echo python script.py --input_path "{input}" --output_path "{output}"
5
Prints the following output:
JavaScript
1
2
1
python script.py --input_path "/folder/input" --output_path "/folder/output/"
2
Obviously, the echo
can be removed to make it actually invoke the python
command, which is what the original question is after.