从另一个 ipynb 文件导入 ipynb 文件? [英] Importing an ipynb file from another ipynb file?

查看:25
本文介绍了从另一个 ipynb 文件导入 ipynb 文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

交互式 Python (ipython) 简直太棒了,尤其是当您动态地拼凑事物时……而且以一种易于返回的方式进行.

Interactive Python (ipython) is simply amazing, especially as you are piecing things together on the fly... and does it in such a way that it is easy to go back.

然而,似乎有趣的是拥有多个 ipython 笔记本(ipynb 文件)的用例.显然笔记本不应该与其他笔记本有关系,这是有道理的,除了我很想导入其他 ipynb 文件.

However, what seems to be interesting is the use-case of having multiple ipython notebooks (ipynb files). It apparently seems like a notebook is NOT supposed to have a relationship with other notebooks, which makes sense, except that I would love to import other ipynb files.

我看到的唯一解决方法是将我的 *.ipynb 文件转换为 *.py 文件,然后可以将其导入到我的笔记本中.让一个文件保存项目中的所有内容有点奇怪,特别是如果我想真正推动代码重用(这不是 Python 的核心原则吗?).

The only workaround I see is converting my *.ipynb files into *.py files, which then can be imported into my notebook. Having one file hold everything in a project is a bit weird, especially if I want to really push for code-reuse (isn't that a core tenet of python?).

我错过了什么吗?这不是 ipython 笔记本支持的用例吗?是否有另一种解决方案可以用于将 ipynb 文件导入另一个笔记本?我很想继续使用 ipynb,但它现在真的搞砸了我的工作流程:(

Am I missing something? Is this not a supported use case of ipython notebooks? Is there another solution I can be using for this import of an ipynb file into another notebook? I'd love to continue to use ipynb, but it's really messing up my workflow right now :(

推荐答案

运行

!pip install ipynb

然后将另一个笔记本导入为

and then import the other notebook as

来自 ipynb.fs.full.导入 *

来自 ipynb.fs.full.导入

确保所有笔记本都在同一目录中.

Make sure that all the notebooks are in the same directory.

编辑 1:您可以在此处查看官方文档 - https://ipynb.readthedocs.io/en/稳定/

Edit 1: You can see the official documentation here - https://ipynb.readthedocs.io/en/stable/

此外,如果您只想导入 class &笔记本中的函数定义(而不是顶级语句),您可以使用 ipynb.fs.defs 而不是 ipynb.fs.full.也将评估完整的大写变量赋值.

Also, if you would like to import only class & function definitions from a notebook (and not the top level statements), you can use ipynb.fs.defs instead of ipynb.fs.full. Full uppercase variable assignment will get evaluated as well.

这篇关于从另一个 ipynb 文件导入 ipynb 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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