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

会有一股神秘感。 提交于 2020-03-26 03:51:19

问题


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:


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)


来源:https://stackoverflow.com/questions/60633533/how-to-call-a-fucntion-from-a-py-file-in-jupyter-notebook

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!