Import a Class from Another File in Python

When I first started working on larger Python projects, I realized that keeping all my code in a single file was messy and hard to maintain.

As my applications grew, I needed a way to organize my code more effectively. That’s when I started splitting classes into separate files and importing them whenever needed.

In this tutorial, I’ll show you multiple ways to import a class from another file in Python. I’ll also explain when each method works best, so you can apply it in your projects confidently.

Why Import a Class from Another File?

When you’re working on real-world projects, especially in the USA, where many companies build large-scale software systems, code organization is critical.

Here are the main reasons I always import classes from other files:

  • Code Reusability – Write once, use in multiple files.
  • Better Organization – Keep related functionality together.
  • Team Collaboration – Easier for multiple developers to work on different modules.
  • Maintainability – Debugging and updating code are simpler.

Method 1 – Import a Class Using the import Statement

The simplest way is to use the import statement.

Let’s say I have two files in the same folder:

File: car.py

# car.py

class Car:
    def __init__(self, make, model, year):
        self.make = make
        self.model = model
        self.year = year

    def display_info(self):
        return f"{self.year} {self.make} {self.model}"

File: main.py

# main.py

import car

# Create an object of the Car class
my_car = car.Car("Ford", "Mustang", 2024)
print(my_car.display_info())

Output:

2024 Ford Mustang

I executed the above example code and added the screenshot below.

python import class from another file

Here, I imported the entire car module and then accessed the Car class using car.Car.

Method 2 – Import a Class Directly Using from … import

If you don’t want to prefix the class with the module name, you can import it directly.

File: main.py

# main.py

from car import Car

my_car = Car("Tesla", "Model S", 2025)
print(my_car.display_info())

Output:

2025 Tesla Model S

I executed the above example code and added the screenshot below.

import class from another file python

This method is cleaner when you only need one class from a file.

Method 3 – Import Multiple Classes from the Same File

Sometimes, a file contains multiple classes. You can import more than one at once.

File: vehicles.py

# vehicles.py

class Car:
    def __init__(self, make, model):
        self.make = make
        self.model = model

    def info(self):
        return f"Car: {self.make} {self.model}"


class Truck:
    def __init__(self, brand, capacity):
        self.brand = brand
        self.capacity = capacity

    def info(self):
        return f"Truck: {self.brand}, Capacity: {self.capacity} tons"

File: main.py

# main.py

from vehicles import Car, Truck

c = Car("Chevrolet", "Camaro")
t = Truck("Ford", 10)

print(c.info())
print(t.info())

Output:

Car: Chevrolet Camaro
Truck: Ford, Capacity: 10 tons

I executed the above example code and added the screenshot below.

python import class

You can easily import and use multiple classes from the same file in Python with a single import statement.

Method 4 – Import All Classes Using *

You can import everything from a file using *.

File: main.py

# main.py

from vehicles import *

c = Car("Dodge", "Charger")
t = Truck("Ram", 12)

print(c.info())
print(t.info())

Output:

Car: Dodge Charger
Truck: Ram, Capacity: 12 tons

While this works, I don’t recommend it for large projects because it can cause naming conflicts and make debugging harder.

Method 5 – Import a Class from a Subfolder

In real-world projects, classes are often stored in subfolders.

Imagine this folder structure:

project/
│
├── main.py
└── transport/
    └── car.py

File: transport/car.py

# car.py

class Car:
    def __init__(self, make, model):
        self.make = make
        self.model = model

    def info(self):
        return f"{self.make} {self.model}"

File: main.py

# main.py

from transport.car import Car

my_car = Car("BMW", "X5")
print(my_car.info())

Output:

BMW X5

This method is very common in structured applications.

Method 6 – Use sys.path.append() for Custom Imports

Sometimes, the file you want to import is not in the same directory. In that case, you can manually add the path.

File Structure:

project/
│
├── main.py
└── utils/
    └── helper.py

File: utils/helper.py

# helper.py

class Helper:
    def greet(self, name):
        return f"Hello, {name}! Welcome to Python projects."

File: main.py

# main.py

import sys
sys.path.append("utils")

from helper import Helper

h = Helper()
print(h.greet("Alice"))

Output:

Hello, Alice! Welcome to Python projects.

This is useful when working with custom directory structures.

Best Practices I Follow

Over the years, I’ve learned these best practices when importing classes:

  • Use explicit imports (from module import Class) instead of *.
  • Keep related classes in the same file, but avoid overloading one file with too many classes.
  • Use packages (__init__.py) for larger projects.
  • Avoid circular imports by carefully planning dependencies.

When I first struggled with imports in Python, I thought it was overly complicated. But once I understood the different methods, it became second nature.

You can start with the basic import and from … import methods, and as your projects grow, move on to packages and structured imports.

Both beginners and experienced developers benefit from mastering this skill, as it makes Python projects cleaner, more maintainable, and easier to scale.

You may also like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.