How to Import Python Files in Google Colaboratory

In this blog, discover how to leverage Google Colaboratory (Colab) for data analysis and machine learning. Explore the platform’s cloud-based Jupyter Notebook environment and learn how to import Python files to enhance your coding experience.

Google Colaboratory, also known as Colab, is a free online platform for data analysis, machine learning, and coding. It provides a cloud-based Jupyter Notebook environment that enables users to write, run, and share Python code. One of the most useful features of Colab is the ability to import Python files. In this article, we’ll explore how to import Python files in Google Colaboratory.

What are Python Files?

Python files are text files with a .py extension that contain Python code. They can be used to store functions, classes, variables, and other objects that can be reused in other Python programs. Python files are an essential part of Python programming and can help you write better, more organized code.

Why Import Python Files in Google Colaboratory?

Google Colaboratory provides a convenient way to write and run Python code in the cloud. However, sometimes you may want to import functions or classes from other Python files. This can be useful when you want to reuse code or when you want to work with a large codebase that’s split into multiple files. By importing Python files in Google Colaboratory, you can save time and effort, increase code reusability, and improve code organization.

How to Import Python Files in Google Colaboratory?

To import Python files in Google Colaboratory, you need to follow the steps below:

Step 1: Upload your Python File to Google Drive

The first step is to upload your Python file to Google Drive. To do this, you can follow these steps:

  1. Open Google Drive in your web browser.
  2. Click on the New button and select File Upload.
  3. Select your Python file and click on Open.

Step 2: Mount Google Drive in Google Colaboratory

The next step is to mount Google Drive in Google Colaboratory. This will allow you to access your Python file from within Colab. To do this, you can run the following code:

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

This will prompt you to authorize Colab to access your Google Drive. Follow the instructions to complete the authorization process.

Step 3: Import Your Python File

Once you’ve mounted your Google Drive, you can import your Python file in Colab. To do this, first locate your python file on the left panel, then you can use the following code to import: Alt text

import sys
# Append the directory to your python path using sys
sys.path.append('/content/drive/MyDrive')
# Import the module
import saturn

The first line adds the path to your Python file to the system path, which allows you to import it. Finally, you can import your Python file as a module using the import statement. You can replace /content/drive/MyDrive to your real path and saturn with your own module.

Let’s say we have this simple code in our saturn.py:

def print_hello():
    print("Hello from Saturn Cloud!")

We can then call a function located inside saturn.py as follow:

saturn.print_hello()

Output:

Hello from Saturn Cloud!

Conclusion

Importing Python files in Google Colaboratory is a useful feature that can help you write better, more organized code. By following the steps outlined in this article, you can easily import Python files in Colab and take advantage of code reuse and organization. We hope this article has been helpful and that you’ll be able to use this feature to improve your Python programming in Google Colaboratory.


About Saturn Cloud

Saturn Cloud is your all-in-one solution for data science & ML development, deployment, and data pipelines in the cloud. Spin up a notebook with 4TB of RAM, add a GPU, connect to a distributed cluster of workers, and more. Request a demo today to learn more.