Open In App

How to Import Custom Modules in Google Colab

Last Updated : 25 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will learn to import our own custom modules in our Google Colab notebook. You can also refer to the video solution for this end which is attached at the end of this article.

What is Google Colab?

Google Colab is a service provided by Google for a lot of researchers and developers around the globe. It is a Jupyter Notebook-like environment in one single place without any prerequisites. It is free to use with a limited number of computer resources and engines including free access to GPUs i.e. Graphics Processing Units for accelerated parallel processing of code. It is one of the best platforms for all students interested in the field of computer science especially machine learning, data science, artificial intelligence, etc. Colab provides a free workspace in the form of Python or R notebooks. These notebooks can be used to implement various compute-heavy tasks in a very easy manner.

Prerequisite to Import Custom Modules in Google Colab

A Google Account. Refer to this article which explains how to create a Gmail account, which is equivalent to creating a Google Account.

Let’s first create a simple function to fetch the current time using Python.

Python3




import datetime
 
def get_current_time():
    current_time = datetime.datetime.now()
    formatted_time = current_time.strftime("%Y-%m-%d %H:%M:%S")
    return formatted_time
 
print(get_current_time())


Output

2023-09-20 10:34:57

There are 3 ways in which this task of importing this custom function to google Colab can be achieved.

Upload the custom function file/folder directly on Colab Notebook

The most simple way to import custom modules google Colab is to directly unpload the file or the folder from our local machines onto colab. We can create a zip file of the folder and upload it as a file on to colab using UI.

Step 1: To upload this file, click on “Files” icon on the left-hand side menu bar, click on “Upload to session storage” icon. It will open up a local system dialog box. Choose the .zip file and it will get uploaded onto colab session.

Screenshot-2023-09-10-at-15916-AM

Step 2: If it is a folder uploaded as a .zip file, then use the following command to unzip the file and hence, we get all the required files onto the colab session storage. Now, all the files from the folders can be easily accessed from colab notebook.

!unzip <file_name>.zip
%ls

Screenshot-2023-09-10-at-13823-AM-(2)

Clone the custom function file/folder as a github repository from github

Secondly, we can create a git repository of the folder or the file and push the code to the remote repository.

Step 1: The repository can be cloned into Colab using the following command to get all the files uploaded onto the Colab notebook.

!git clone <repository-link>
%ls

Screenshot-2023-09-10-at-15946-AM

This way we need not directly upload our files on colab notebook, rather we keep all our files on a remote repository and then fetch it into our notebook.

Upload custom function file/folder on Google drive and Mount it on Colab

This is one of the most common ways to get the required files and the corresponding custom modules into our Colab session storage. This way, we need not upload our files everytime onto the colab, rather we would just upload it only once on google drive and then mount the drive storage into our colab notebook any number of times using the following lines of code. In order to mount our drive, we need to give permission to google colab to access the drive storage.

Screenshot-2023-09-10-at-20020-AM

Step 1: Use the following piece of code in order to connect to your google drive using colab notebook.

from google.colab import drive
drive.mount('/content/drive')
cd path/to/your/folder
%ls

Screenshot-2023-09-10-at-20054-AM

By following the above steps, we can easily upload our file or the folder which contain the custom functions to the colab notebook.

How to unmount drive in Google Colab

The already mounted drive on google colab can be easily unmounted by running the following command.

from google.colab import drive
drive.flush_and_unmount()

This command will unmount the the already mounted drive and then using the command given in the previous section of the article, we can easily remount another drive onto our colab notebook after disconnecting the session and reconnecting it.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads