我正在尝试从另一个jupyter笔记本导入一个函数
在n1.ipynb:
# ./app/decorators/base.rb
module Base
# methods defined here
end
# ./app/decorators/post.rb
class PostDecorator < ApplicationDecorator
delegate_all
include Base
end
# ./app/decorators/comment.rb
class CommentDecorator < ApplicationDecorator
delegate_all
include Base
end
在n2.ipynb:
def test_func(x):
return x + 1
-> run this
错误:
%%capture
%%run n1.ipynb
test_func(2)
任何简单的方法都可以吗?
答案 0 :(得分:9)
nbimporter模块在这里帮助我们:
pip install nbimporter
例如,在此目录结构中有两个笔记本:
/src/configuration_nb.ipynb
analysis.ipynb
/src/configuration_nb.ipynb:
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文件导入和使用模块。
/src/configuration.py
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