简体   繁体   English

如何从 Jupyter Notebook 中的 .py 文件调用功能?

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

I don't want to write the same function in each Jupyter Notebook files.我不想在每个 Jupyter Notebook 文件中编写相同的函数。 It would be easier if I just need to edit the function once without the need to edit in each .ipynb file.如果我只需要编辑一次函数而不需要在每个 .ipynb 文件中编辑,那会更容易。

The problem is, I have to restart kernel if I edit the .py file which will re-start everything.问题是,如果我编辑 .py 文件将重新启动一切,我必须重新启动内核。

Is there any way I can simply call a function from several .pynb files?有什么办法可以简单地从几个 .pynb 文件中调用一个函数?

PS: After I have edited the .py, it does not affect the .ipynb file. PS:我编辑完.py后,对.ipynb文件没有影响。

Yes, you need a make .py file with function defined and import that file then call the functions in it.是的,您需要一个定义了函数的 make .py 文件并导入该文件,然后调用其中的函数。 put it in the same folder as notebooks.把它和笔记本放在同一个文件夹里。

the filename.py file example filename.py 文件示例

def function_name(param1,param2):
    #dosomting
    return

and how to import it以及如何导入

from filename import function_name

function_name(param1,param2)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM