'How to run an external python script in a Jupyter notebook when passing strings as arguments?

I'm trying to run an external python file from a Jupyter notebook. The external python file takes a file path as an argument. I'd like to know how to pass this file path to the external .py file being run, from a string variable in the Jupyter notebook.

The following is what I've tried, and the error message which is returned:

path = '/Users/documents/'
%run script.py path

Which returns:

IndexError: list index out of range

However I get the expected results if I run:

%run script.py /Users/documents/

I'd like to be able to store the string as a variable for ease of use (not having to edit the file path in multiple parts of a notebook. Is there an easy solution?



Solution 1:[1]

You should put your variable inside { }. Something like this:

%run script.py {path}

I searched few hours until I found it here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mehdi