Google Colab is just like a Jupyter Notebook that lets you write, run and share code within Google Drive. A notebook document is composed of cells, each of which can contain code, text, images, and more.
Colab connects your notebook to a cloud-based runtime, meaning you can execute Python code without any required setup on your own machine. Additional code cells are executed using that same runtime, resulting in a rich, interactive coding experience in which you can use any of the functionality that Python offers.
The notebook is stored in the standard Jupyter Notebook format, and it can be viewed and executed in Jupyter Notebook, JupyterLab, and other compatible frameworks.
.py python file can be referenced as a module. This tutorial will guide you through writing python modules for use within Google Colab.
Let’s start by creating a file
greeting.py that we’ll later import into Colab.To begin, we’ll create a function that prints Hello, World!:
def hello(): print("Hello, World!")
Upload Python Module
Google Colab is stored on Google Drive. But it is run on another virtual machine. You should not upload it to google drive. You should upload it to Colab instead. You need to copy your
greeting.py there too. Do this to upload
greeting.py through Colab.
from google.colab import files files.upload()
Select the files for upload.
We can import the module that we just uploaded, and then call the function. This file in the same directory so that Python knows where to find the module since it’s not a built-in module.
import greeting greeting.hello()
Because we are importing a module, we need to call the function by referencing the module name in dot notation.
Execute Python Script
If you have an executable python file inside the Google Colab, you can run it using the following command. To run a script
You can run any python script on top of a GPU provided by Google Colab. The scripts include common functions that carry out general utility file handling, accuracy metrics, plotting, etc. For scripting, you can use an IDE such as PyCharm, Visual Code or Spyder.