Skip to main content

Google Colab is probably one of the easiest tools to start working with python codes: among its best features we mention that it’s intuitive, it gives the opportunity to add text, images and links to create a more dynamic and readable script. It’s a Google product, so it’s completely free and doesn’t require any installation as it comes as a web service. For the same reason, it also allows to share codes and work on them simultaneously.

One of the pitfalls though is that it might seem that it is not possible to split a project in multiple files and import the appendices in the main as modules and libraries.
It is not the truth! Thus, the aim of this quick guide will be to show how to import pyhton modules and libraries into Google Colab notebooks in three easy steps!

Let’s go through them together!

Step 1: Synchronize Drive with Colab

The first step consists in mounting your Google Drive in Google Colab. In this way, you will provide a location where to physically store the files you want to access from your notebook.

Here there’s a simple command to do it:

# Mount your Google Drive in Google Colab

from google.colab import drive
drive.mount('/content/drive')

Step 2: Upload the files on your Drive

At this point, we have to upload our files in a directory in Google Drive to later provide in the Colab notebook the right path to access it.

We can load the scripts manually, by dragging/uploading them in the folder of pertinence or we could call them directly from Colab as it follows:

# Upload files in Google Drive using Colab

from google.colab import files
files.upload()

Since we would like to keep our project clean, ideally we would upload our appendix files in the same directory as our main, which by default is stored in the Colab Notebook folder of our Drive.
But Colab reads its information from a deeper level, specifically at the one we have defined in Step 1 to mount our drive: ‘/content/drive’.

Step 3: Allow Colab to read the files

We provide two possible solutions: the first a bit more hurried, the latter more orderly.
The brutal solution is to copy each of the python files from the directory they are stored in to the one were they could be read as modules. The command is pretty straightforward: !cp /“path of the file to copy” /”path of where to store it for Colab“.
Here’s a practical example:

# Copy the children files in the content folder:
!cp /content/drive/MyDrive/Colab\ Notebooks/example.py /content

Note that if some directories in the path contain spaces in their name the backslash must be use as an escape. In its absence, the space will be considered as the end of the path of the element to copy.
This procedure is fine… but what if we have a lot of scripts with different functions and structures? We cannot really copy them one by one, there has to be a smarter way.

Indeed, there is. And it goes like this:

# Provide Colab a systemic path: 
import sys
sys.path.insert(0,’/content/drive/My Drive/ColabNotebooks’)

This way allows us to define only once the path where all the files are stored (the use of apostrophes will also allow us to insert it as a string and not to care about how we define spaces) and makes our life way simpler!

Finally

Now, we just have to import our libraries and use the function stored in them:

import example

x = example.my_function()

Easy, right? So, what are you waiting for? It’s your turn to try it out on Colab!

your turn to import python scripts into Colab!

Have questions about data? Be sure to check our blog.

Auteur