本文介绍了如何在Google Colab中将Jupyter笔记本的功能导入另一个Jupyter笔记本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将Jupyter笔记本的功能(以.ipynb结尾)导入另一个Jupyter笔记本.

I would like to import functions of a Jupyter notebook (ending .ipynb) into another Jupyter notebook.

两个笔记本均位于同一文件的Google云端硬盘中.应该在其中导入其他笔记本的功能的笔记本已经在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库.

首先,安装您的Google驱动器以访问您的xxx.ipynb

First, mount your google drive to access your xxx.ipynb

from google.colab import drive
drive.mount("mnt")

然后将目录更改为笔记本目录.

Then change directory to the notebook directory.

%cd "mnt/My Drive/Colab Notebooks"

现在安装import_ipynb库,并将其导入

Now install the import_ipynb library, and import it

!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笔记本的功能导入另一个Jupyter笔记本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 05:34