Running Your Complete Python Project in Google Colab

Sarthak
4 min readOct 22, 2023

--

No more hassle of setting up complex environments and running hefty machine learning projects on your local machine. The power of cloud environments is at your fingertips, waiting to be harnessed. In this blog, I’ll guide you through the process of setting up and running machine learning projects seamlessly on cloud notebooks, unlocking the true potential of cloud-based development.

Introduction

Google Colaboratory, is a powerful cloud-based platform that allows you to run Python code without the need for any local installations. It’s an invaluable tool for data scientists, machine learning engineers, and anyone who needs a free and easily accessible environment to develop and execute Python projects. In this blog post, we’ll guide you through the process of running Python projects on Google Colab.

Need of this blog?

  • No Clear Guide: While there are many tutorials on individual aspects of Google Colab, there’s a shortage of resources that guide you through the complete process of setting up an environment, organizing your project, and executing it from start to finish.
  • Environment Configuration: Configuring the environment for your project is crucial. You need to install libraries, manage dependencies, and set up access to data. A lack of clear guidance can lead to confusion.
  • Project Organization: Knowing how to structure your project files and ensure that all your scripts and data are easily accessible in the Colab environment is essential for productivity.

Getting Started

  1. Create a New Notebook — Once you’re logged in, go to Colab. You can create a new notebook by clicking on “File” > “New Notebook” or simply use “Ctrl+Enter.”
  2. Clone the project — There are two options either you can upload the entire python project to the google drive or clone the GitHub project.
!git clone "project github url"

To mount the drive, you can use the following command:-

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

3. Navigate to the working directory!

%cd "/content/directory_name"

4. Set up the environment if required

If you are required to set up conda env then perform the following commands in kernel.

!wget -c https://repo.continuum.io/archive/Anaconda3-5.1.0-Linux-x86_64.sh
!chmod +x Anaconda3-5.1.0-Linux-x86_64.sh
!bash ./Anaconda3-5.1.0-Linux-x86_64.sh -b -f -p /usr/local
!conda upgrade -n base conda
!conda env create -f environment.yml # Optional if conda env setup is present in environment.yml

Else, if you required to set up normal python env perform the following commands: -

!pip install virtualenv
!virtualenv "env_name" # To set up the env

!source /content/myenv/bin/activate; pip install # To activate the environment and download all its dependencies and packages

But the problem with Colab is you can’t permanently switch to another environment, hence, to perform commands inside particular environment, you need to activate env before each command as shown: -

!source /content/myenv/bin/activate; pip3 list 

5. The virtual environment is set up and all its dependencies are downloaded, you can use the project files directly into the colab and run the project.

In Google Colab, you can’t run Python files directly, but thanks to the environment’s setup, you can seamlessly work with your code. By copying and pasting your code into Colab cells, it functions as if it were a separate Python file running within the working directory. This approach allows you to compartmentalize your code, test individual sections, and execute your Python project in a structured manner. The result is a smooth and effective workflow, combining the best of both traditional file-based development and the cloud-based convenience of Colab.

Advantages

  1. Free GPU and TPU Access: Google Colab offers free access to Graphics Processing Units (GPUs) and Tensor Processing Units (TPUs), making it an excellent choice for machine learning and data science projects that require significant computational power. This can accelerate model training and data processing.
  2. No Setup Hassles: You can start working on your Python projects in Colab without the need to set up a local development environment. Colab comes pre-configured with essential libraries, making it accessible and convenient for both beginners and experienced developers.
  3. Cloud-Based Collaboration: Google Colab is a cloud-based platform that supports real-time collaboration with team members or collaborators. You can easily share and work on Colab notebooks, which is particularly useful for group projects and collaborative coding.

Conclusion

This blog was created to fill a significant gap in the availability of comprehensive resources on running Python projects in Google Colab. Our aim is to provide you with the knowledge and guidance you need to successfully set up your project, configure the environment, and execute it efficiently on this powerful cloud-based platform. Whether you’re a novice or an experienced developer, this guide will empower you to harness the full potential of Google Colab for your Python projects. Stay tuned for the upcoming blog post, where we will dive into the practical steps to make your Python projects run seamlessly on Google Colab.

For any queries, reach out to me at LinkedIn.

Happy Coding!

--

--

Sarthak

Passionate undergraduate student at Punjab Engineering College, India, exploring the world of machine learning.