16

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)
Code Now
  • 711
  • 2
  • 9
  • 20

3 Answers3

29

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.

Update (oct 2020)

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()

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()
korakot
  • 37,818
  • 16
  • 123
  • 144
  • Is there any other solutions without mounting the derive? – KKK Apr 07 '20 at 10:19
  • 1
    I was previously doing this: I would convert and download my `My_Custom_Functions.ipynb` as `my_custom_functions.py` and reupload it back to my drive Colab Notebooks. This was a long story and I did not like but it stopped working at `!pip install one_module`. This failed. After searching, I found your answer. Surprisingly, it worked like a cham. Thanks a lot for saving my time and new lesson. – Mainland Aug 16 '22 at 14:46
  • I'm trying the first approach, I can import the file, but when it comes to calling the function. For example A.function_call(), I get the following: AttributeError: module 'A' has no attribute 'function_call' – disruptive Jun 10 '23 at 09:39
1

The simplest way:

  1. Mount Google Drive
  2. Install the ipynb module
  3. cd to your working directory
  4. Run from ipynb.fs.defs.<the notebook name> import <the things you want to import>

Example:

!pip install ipynb
%cd /your/working/directory
from ipynb.fs.defs.notebook_to_import import class1, class2, func1, func2
Vu Nguyen
  • 52
  • 5
0

You Can follow below method without changing the directory:

source_path_file = '/content/drive/My Drive/Colab Notebooks/Works/functions.ipynb'
source_path_file = source_path_file.replace(' ', '\\ ')

!cp $source_path_file '/content' # to copy the file from drive to colab

!rsync -aP $source_path_file '/content/functions.ipynb' # run this line to sync with the parent file in case you made any changes

import import_ipynb
import functions as fn
#as i'm importing functions.ipynb in my case

If you want to import functions file without mounting to drive. You are gonna have to import the functions file from some where and you can follow above from 'imports sections'.