How to Run a Python Script in a py File from a Google Colab Notebook
As a software engineer, you might be aware of the importance of using a notebook environment to run your code seamlessly. Google Colab is one of the most popular online notebook environments that allows you to write and run your code in a browser. In this tutorial, we will explore how to run a Python script in a ‘.py’ file from a Google Colab notebook.
What is Google Colab?
Google Colab is an online notebook environment that allows you to run Python code in a browser. It is a cloud-based service that provides access to a free GPU and TPU for running machine learning models. Colab notebooks are based on Jupyter notebooks and allow you to write and execute code in a variety of languages, including Python, R, and Julia.
Uploading a ‘.py’ File to Google Colab
The first step in running a Python script in a ‘.py’ file from a Google Colab notebook is to upload the file to the Colab environment. You can do this by following these simple steps:
- Open a new Colab notebook or an existing one.
- Click on the “Files” button on the left-hand side of the notebook.
- Click on the “Upload” button and select the ‘.py’ file you want to upload.
- Once the file is uploaded, you can see it in the “Files” section.
Running a Python Script from a ‘.py’ File in Google Colab
Now that you have uploaded the ‘.py’ file to Google Colab, you can run the Python script using the following steps:
- Open a new cell in the Colab notebook by clicking on the “+” button at the top left corner of the page.
- In the new cell, type the following command, replacing “file_name.py” with the name of the ‘.py’ file you uploaded:
!python file_name.py
- Press the “Shift” + “Enter” keys to execute the cell.

Conclusion
Google Colab is a powerful tool for running Python code in a browser. In this tutorial, we have shown you how to run a Python script in a ‘.py’ file from a Google Colab notebook. By following these simple steps, you can seamlessly integrate your existing Python code with Google Colab and take advantage of its powerful features.

