如何从Jupyter Notebook中的.py文件调用功能? [英] How to call a fucntion from a .py file in Jupyter Notebook?

查看:2685
本文介绍了如何从Jupyter Notebook中的.py文件调用功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不想在每个Jupyter Notebook文件中编写相同的功能.如果只需要对函数进行一次编辑而无需在每个.ipynb文件中进行编辑,那会更加容易.

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.

问题是,如果我编辑.py文件将重新启动所有程序,则必须重新启动内核.

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

有什么方法可以简单地从几个.pynb文件中调用一个函数吗?

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

PS:编辑完.py后,它不会影响.ipynb文件.

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

推荐答案

是的,您需要一个定义了函数的make .py文件,然后导入该文件,然后在其中调用函数.放在与笔记本相同的文件夹中.

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.

filename.py文件示例

the filename.py file example

def function_name(param1,param2):
    #dosomting
    return

以及如何导入

from filename import function_name

function_name(param1,param2)

这篇关于如何从Jupyter Notebook中的.py文件调用功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆