我试图从另一个jupyter笔记本导入功能
在n1.ipynb:
def test_func(x):
return x + 1
-> run this
在n2.ipynb:
%%capture
%%run n1.ipynb
test_func(2)
错误:
NameError Traceback (most recent call last)<ipython-input-2-4255cde9aae3> in <module>()
----> 1 test_func(1)
NameError: name 'test_func' is not defined
任何简单的方法来做到这一点吗?
该nbimporter模块帮助我们在这里:
pip install nbimporter
例如,在该目录结构两款笔记本:
/双人床/configuration_你不.IP也NB
analysis.ipynb
/双人床/configuration_你不.IP也NB:
class Configuration_nb():
def __init__(self):
print('hello from configuration notebook')
analysis.ipynb:
import nbimporter
from src import configuration_nb
new = configuration_nb.Configuration_nb()
输出:
Importing Jupyter notebook from ......\src\configuration_nb.ipynb
hello from configuration notebook
我们也可以导入和使用的模块从Python文件。
/双人床/configuration.朋友
class Configuration():
def __init__(self):
print('hello from configuration.py')
analysis.ipynb:
import nbimporter
from src import configuration
new = configuration.Configuration()
输出:
hello from configuration.py