3 Approaches to Import .py Files in Google Colab

IMPORTING PYTHON FILES IN GOOGLE COLABORATORY

Google Colab is one of the most used online tools by machine learning and data sciences professionals, students, and enthusiasts. Google Colab is known to provide a collaborative environment to support programming in Python, C, C#, Java, HTML, and LaTeX.

It is a Jupyter Notebook but hosted on the cloud, so we can use it without any installation or storage hassles. One of the main reasons many developers prefer Google Colab is due to its collaborative feature. Your team members can simultaneously make changes to the code you have written while you are still developing (only with permission, of course).

Other perks of Google Colab are the pre-installed vast libraries. You don’t need to manually install all the extensive Python libraries like Pandas, Numpy, Matplotlib, etc. They are all already available in the Colab environment and can just be imported for use.

Unlike traditional Python scripts, which are saved with a .py extension, the code we write in a Colab notebook is saved with a .ipynb extension, which stands for IPython Notebook.

It also supports Cloud GPU and TPU services to reduce the burden on the local storage or CPU while running high-end and costly models like Convolutional Neural Networks(CNNs) and other deep learning models.

Related: Deep Learning algorithms to learn in 2023

AI is everywhere. Following this path, Google Colab has also incorporated AI into its environment. AI is now being used in Google Colab to provide interactive visualizations of the data we want.

Writing code in Colab is very simple, but sometimes we might need to import files from other environments. You might need to use a certain function from a particular Python file; instead of rewriting the entire code, you can simply import the Python file and just call the function you need thereafter.

Let us take a look at the possible approaches to importing Python files in Google Colab.

Import the Python File From the Local Storage in Google Colab

Imagine, you have the Python file ready in your local storage area but are clueless about how to bring this Python file to the Colab coding environment.

You can actually do this with one click using the Upload option of the Google Colab interface.

Here’s how you do it- Open Google Colaboratory and select New Notebook. There is a tab called Files.

Google Colab Interface
Google Colab Interface

There you will find an Upload icon, Click on it and a pop-up will appear to upload the file you want.

Upload Icon
Upload Icon

After successful upload, you can see the preview of the file as shown below.

File Preview
File Preview

Now that we have the Python file in our environment, let us look at the variables present in the Python code.

We can use the magic commands to run the imported file and also display the variables present in the file.

The command to run the file is given below.

%run myfile.py

This command will print the output of the code.

Run the Imported Python File
Run the Imported Python File

To display the variables, there are two methods: %who and %whos. The first command will just list out the variable names, whereas the second command will display the variables and some additional information.

%who 
%whos
who vs. whos
who vs. whos

Mount the Google Drive

Instead of uploading the file from your local storage, if you want to completely depend on the cloud, there is another option to store the file on your drive and then mount your Google Drive in the colab environment.

There is a two-line code to mount the Google Drive which is given below.

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

With this code we can mount the google drive in seconds.

Mounting the Google Drive
Mounting the Google Drive

Now that the file is present in our environment, let us use the function from the file.

import sys
sys.path.append('/content/drive/MyDrive')
from myfile import add_a_b
result = add_a_b(10, 20)
print("Result:", result)

We are storing the path of the mounted drive with the help of the sys module. Then, we import the function add_a_b from the code (it performs the addition of two variables). The result computed by this function is printed on the next line.

Importing the function
Importing the function

Import the Python File in a Github Repo to Google Colab

Suppose you want to import a file present in a GitHub Repository into your Colaboratory. Instead of downloading it from the repo and uploading it in colab, there is a simple command to access the repo in which the file is located.

!wget https://raw.githubusercontent.com/gnv11/testpy/main/testfile.py
from testfile import add_a_b
result = add_a_b(10,20)
print("Result:",result)

The wget command is used to access any file present in a URL. The repository called testpy of the user gnv11 has a file called testfile. We are importing the function present in the file and printing its computation result.

Importing file from GitHub Repo
Importing files from GitHub Repo

Conclusion

To summarize what we did, we looked at the numerous advantages of Google Colaboratory and why it is used extensively. It can be coined as the one-stop destination for machine learning and data science enthusiasts who always work on projects.

We looked at the possible approaches to importing Python files from local storage on other platforms, like GitHub.

After importing the files, we also looked at how to run the file, how to display the variables present, and how to use the functions present in the file.

References

Google Colab

Magic Comands