How to Import from a Python File in Google Drive to a Colab Notebook (in 4 Simple Steps)

1 min read
Step 1: Mount Google Drive
from google.colab import drive
drive.mount('/content/drive')
This connects your Google Drive to the Colab environment.
Step 2: Verify the File Path
import os # Imports Python's built-in os module for interacting with the file system
# List files inside the target folder
os.listdir("/content/drive/MyDrive/Colab_Notebooks")
This helps confirm your
.py
file is located where you expect it to be. Make sure the filename appears in the output.
Step 3: Add Your Module's Folder to Python Path
import sys
sys.path.append('/content/drive/MyDrive/Colab_Notebooks')
Adjust the path if your
.py
file is in a subfolder.
Step 4: Import Your Python File as a Module
import your_file_name # Do not include the `.py` extension
Now you can use:
your_file_name.your_function()
Congratulations…!!! :)
1
Subscribe to my newsletter
Read articles from Madara Wimalarathna directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Madara Wimalarathna
Madara Wimalarathna
I'm currently pursuing a Master's by Research in machine learning, and sharing my journey and learnings along the way.