Issue
I’m working with google-colab.
Can it be possible to call a function from one colab file in to another colab project, as we do in python like importing file.
ex.
I have two colab files 1.ipynb and 2.ipynb.
Let’s say 1.ipynb has ABC function.
Now I want to utilize this already implemented ABC function into 2.ipynb
is there any way I can do it? or it’s not possible?
Solution
It’s possible using the import-ipynb libary:
!pip install import-ipynb
import import_ipynb
This lets you import code from .ipynb files in the same way you would from .py files – simply by calling import filename
or from filename import function, class, etc.
(which is more advisable).
Assuming you have your notebooks neatly organized on your Google Drive, eg. in MyDrive/Colab_Notebooks/
you can then mount it:
from google.colab import drive
drive.mount('/content/drive')
and import the functions you want from the source notebook:
from drive.MyDrive.Colab_Notebooks.notebook1 import ABC, someOtherFunction
Answered By – Jacek Jaskólski
Answer Checked By – Clifford M. (BugsFixing Volunteer)