How to call a fucntion from a .py file in Jupyter Notebook?

Viewed 2670

I don't want to write the same function in each Jupyter Notebook files. It would be easier if I just need to edit the function once without the need to edit in each .ipynb file.

The problem is, I have to restart kernel if I edit the .py file which will re-start everything.

Is there any way I can simply call a function from several .pynb files?

PS: After I have edited the .py, it does not affect the .ipynb file.

1 Answers

Yes, you need a make .py file with function defined and import that file then call the functions in it. put it in the same folder as notebooks.

the filename.py file example

def function_name(param1,param2):
    #dosomting
    return

and how to import it

from filename import function_name

function_name(param1,param2)
Related