Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Colab - Call function from another .ipynb file

I have a local (not in Colab) Jupyter notebook that calls a function from another Jupyter notebook, it works fine. I've used David Rinck's answer from here: import a function from another .ipynb file and used this line to import the function: from ipynb.fs.full.MyFunctions import MyFunction

I've imported these two notebooks into Colab and when I run the main notebook I get the following error: No module named 'ipynb.fs.full.MyFunctions'

What have I missed in the process of importing to Colab? (I've run !pip install ipynb in Colab as well)

like image 884
Itai Avatar asked Jul 05 '19 13:07

Itai


People also ask

How do you call a function from a different file in Colab?

If you expand the files tab in Colab you can navigate to the files in Google Drive. Then you can right click on the file or folder you want and do Copy path . You can then paste this into the cell where path_to_module is defined.

Can you call functions from other Jupyter notebooks?

py file), or Jupyter Notebook. Remember the file that contains the function definitions and the file calling the functions must be in the same directory. To use the functions written in one file inside another file include the import line, from filename import function_name .

How do I import a function from another Python file into Databricks?

To import from a Python file, see Reference source code files using git. Or, package the file into a Python library, create an Azure Databricks library from that Python library, and install the library into the cluster you use to run your notebook.


1 Answers

You can use the import-ipynb package.

Starting by installing:

!pip install import_ipynb
import import_ipynb

Then,

 import "your .ipynb file"

For Example, I have a sqroot.ipynb file with a function

def sqroot(x):
  return x**.5

We can run this function using the following:

import sqroot
sqroot.sqroot(2)
like image 168
Mohana Avatar answered Sep 22 '22 20:09

Mohana