How to import functions of a jupyter notebook into another jupyter notebook in Google Colab

只愿长相守 提交于 2020-01-05 07:24:11

问题


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

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

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)

回答1:


You can use import_ipynb library.

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"

Now install the import_ipynb library, and import it

!pip install import-ipynb
import import_ipynb

Now you can import your xxx.ipynb

import xxx
xxx.do_something()

Here's an example Colab.



来源:https://stackoverflow.com/questions/59020008/how-to-import-functions-of-a-jupyter-notebook-into-another-jupyter-notebook-in-g

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!