如何在 Google Colab 中将 jupyter notebook 的功能导入另一个 jupyter notebook [英] How to import functions of a jupyter notebook into another jupyter notebook in Google Colab
问题描述
我想将一个 Jupyter Notebook(以 .ipynb 结尾)的功能导入另一个 Jupyter Notebook.
I would like to import functions of a Jupyter notebook (ending .ipynb) into another Jupyter notebook.
两个笔记本都位于 Google Drive 的同一个文件中.需要导入其他笔记本功能的笔记本已在 Google Colab 中打开.
Both notebooks are located in Google Drive in the same file. The notebook in which the functions of the other notebook should be imported, is already open in Google Colab.
因此我正在寻找像这样剪断的代码
Therefore I'm looking for a code snipped like
from xxx.ipynb import functionX
我已经安装了 PyDrive 包装器并验证并创建了 PyDrive 客户端,如下所示:
I have already installed the PyDrive wrapper and authenticated and created the PyDrive client like follows:
!pip install -U -q PyDrive
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from google.colab import auth
from oauth2client.client import GoogleCredentials
auth.authenticate_user()
gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default()
drive = GoogleDrive(gauth)
推荐答案
您可以使用 import_ipynb
库.
首先,挂载你的谷歌驱动器来访问你的xxx.ipynb
First, mount your google drive to access your xxx.ipynb
from google.colab import drive
drive.mount("mnt")
然后将目录更改为 notebook 目录.
Then change directory to the notebook directory.
%cd "mnt/My Drive/Colab Notebooks"
现在安装import_ipynb
库,并导入
!pip install import-ipynb
import import_ipynb
现在你可以导入你的xxx.ipynb
import xxx
xxx.do_something()
这是一个示例 Colab.
我简化了这个过程,只需安装 kora
并调用一个函数.
I have made the process easier, by just installing kora
and call a function.
(也请在新笔记本中使用自动挂载)>
(Please also use auto-mount in a new notebook)
!pip install kora -q
from kora import drive
drive.link_nbs()
现在您可以从您之前制作的任何笔记本中导入.例如,如果你已经有 mylib.ipynb
你可以
Now you can import from any notebooks you made before. For example, if you already have mylib.ipynb
you can
import mylib
mylib.do_something()
这篇关于如何在 Google Colab 中将 jupyter notebook 的功能导入另一个 jupyter notebook的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!