Import Classes From Another Python File or Package

Quick answer: Put the class in a module file, then import the module or the class using the project package path. Keep the file layout and execution context consistent so Python can resolve the import without modifying sys.path at runtime.

Python class import diagram showing a module owner package import path python -m execution and circular import handling
A reliable class import comes from a stable package layout and a consistent way of running the code.

To import a class from another Python file, import it from the module where the class is defined. A file named models.py is imported as the module models.

The main references are Python’s modules tutorial, the import system reference, and the packages tutorial.

Imports work best when your project has a clear structure. Keep reusable classes in modules, then import those classes from scripts, tests, or other modules that need them.

Avoid changing sys.path as the first solution. Most import problems are better fixed by running the project from the correct folder or using a package layout.

Import From A File In The Same Folder

If two files are in the same folder, import the class from the other file’s module name.

# models.py
class User:
    def __init__(self, name):
        self.name = name

    def greet(self):
        return f"Hello, {self.name}"

# app.py
from models import User

user = User("Ada")
print(user.greet())

The file name is models.py, so the module name is models.

Run app.py from the folder that contains both files.

This pattern is simple and works well for small scripts.

If Python cannot find the module, check the command you used to run the script and the folder where the command started.

Import The Module Instead

You can import the module and access the class through the module name.

# app.py
import models

user = models.User("Grace")
print(user.greet())

This makes it obvious which module owns the class.

Module imports are useful when a file contains several related classes or functions.

This style can also make tests easier to patch because the module ownership stays visible in every call.

Python Pool infographic showing Python files, modules, packages, classes, and import names
Module boundary: Python files, modules, packages, classes, and import names.

Use A Package Folder

For larger projects, place modules inside a package folder. Add __init__.py when you want a regular package.

# project layout
# app.py
# shop/__init__.py
# shop/models.py

from shop.models import Product

item = Product("Book")
print(item.name)

The import path follows the package and module names: shop.models.

Package imports are easier to maintain as the project grows.

A package folder lets you group related modules under one namespace. That reduces name clashes and makes imports clearer in larger projects.

Expose A Class From init

You can re-export a class from __init__.py to make imports shorter.

# shop/__init__.py
from .models import Product

# app.py
from shop import Product

item = Product("Notebook")
print(item.name)

This is useful for public package APIs. Keep __init__.py lightweight so importing the package does not trigger heavy work.

Use explicit re-exports only for names you want callers to use directly.

Do not import every internal class into __init__.py. A small public surface is easier to document and less likely to create circular imports.

Python Pool infographic mapping a project root, package __init__, module path, and class symbol
Import path: A project root, package __init__, module path, and class symbol.

Use An Alias For Name Clashes

If two modules define classes with the same name, import one with an alias.

from local_models import User as LocalUser
from remote_models import User as RemoteUser

local = LocalUser("Ada")
remote = RemoteUser("Grace")

print(local.name)
print(remote.name)

Aliases keep code readable when two imported names would otherwise collide.

Choose aliases that describe the role, not just shorter names.

Aliases are especially helpful in migration code, tests, and adapters where two systems expose classes with the same name.

Python Pool infographic comparing direct class imports, package exports, aliases, and public API
Export classes: Direct class imports, package exports, aliases, and public API.

Fix ModuleNotFoundError

If an import fails, confirm the module file exists and that Python is running from the expected project root.

Also check file names carefully. A local file named like a standard-library module can shadow the real module and create confusing import behavior.

For packages, prefer absolute imports from the package root in application code. Relative imports are useful inside packages, but they require the module to be run as part of the package.

Avoid Circular Imports

A circular import happens when two modules import each other while Python is still loading them.

# Better structure
# models.py defines data classes
# services.py imports models
# app.py imports services

from services import create_user

user = create_user("Ada")
print(user.name)

Move shared classes to a lower-level module that both sides can import. Keep top-level imports pointed in one direction.

The practical rule is to import from modules and packages, not from file paths. If imports fail, check the working folder, package layout, and circular dependencies before editing sys.path.

For repeatable projects, run modules with python -m package.module from the project root when package imports are involved.

Keep import statements at the top of the file unless a local import is needed to break a circular dependency or delay an optional dependency.

When sharing a project, include a short README command that shows how to run it from the project root. That prevents many import errors caused by starting from the wrong folder.

If a class is used across many modules, place it in a stable module with a clear name. That keeps imports consistent as the project grows.

Python Pool infographic testing circular imports, naming, working directory, and interpreter path
Import checks: Circular imports, naming, working directory, and interpreter path.

Import A Class From A Module

If models.py defines User beside main.py, a simple script can import the class by module name.

# models.py
class User:
    pass

# main.py
from models import User

user = User()
print(type(user).__name__)

For a package, import through the package name instead of depending on the current terminal directory.

from myapp.models import User

Run Packages Consistently

Relative imports such as from .models import User require the file to run as part of a package. Start the package with python -m package.module or install the project in its environment rather than executing an internal file as an unrelated script.

Avoid circular imports by keeping shared classes in a lower-level module, and do not solve an import problem by appending arbitrary paths to sys.path. That can hide a packaging error and make the program behave differently in tests, production, and an IDE.

For project imports, compare Python classes versus modules with conditional import patterns. Read python class vs module and python conditional import for the related workflow.

Frequently Asked Questions

How do I import a class from another Python file?

Define the class in a module and import it with its module path, such as from models import User when both modules are in the same importable package context.

How do I import a class from a package?

Use the package path, such as from app.models import User, and run the project from a context where the package root is importable.

Why does an import work in one directory but not another?

Python resolves imports from the execution context and import path, so use a stable package layout and run a package entry point with python -m when appropriate.

How do I fix a circular import?

Move shared types to a lower-level module, restructure dependencies, or use a local import only when the dependency genuinely needs to be deferred.

Subscribe
Notify of
guest
2 Comments
Oldest
Newest Most Voted
Max
Max
4 years ago

Put a __init__.py file under the directory which has the py files you’re making the import statements.

Pratik Kinage
Admin
4 years ago
Reply to  Max

Yes!