How to run .py files (python scripts) on Google Colab

--

We all know how to run jupyter notebook(.ipynb) files on Google colab. Python script files can also be run on Google colab by a very simple trick. This is a very useful trick, as it enables you to make use of the GPU/TPU provided by Colab to run your .py file.

First, you have to create a new notebook or open an existing one.

Then, click on this icon(shown in red arrow) to upload files.

Upload your .py files.

Insert a code cell in the notebook and write a line in this format :

!python filename.py

Execute the cell. You will see output as usual.

You can also view and edit an uploaded python file by double-clicking the filename in files section.

--

--

Rafat Ashraf Joy
Rafat Ashraf Joy

Written by Rafat Ashraf Joy

Computer Science PhD Student at University of Illinois Chicago

Responses (1)