Importing class from another file
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Importing a class from another file in Python is straightforward when your project structure and execution method are consistent. Most import problems come from running files directly from the wrong location, not from Python syntax itself. A package-first layout with absolute imports is the most reliable approach for applications and reusable libraries.
Project Layout That Supports Clean Imports
Use a package directory with __init__.py files so Python treats folders as importable modules.
Define your class in one file and import it from another.
This absolute-import style remains stable as the codebase grows.
Run Modules Correctly
How you start Python determines import resolution behavior. From the project root, run modules with -m so package context is preserved.
If you run a file directly by path, imports that work in package mode may fail.
A minimal entry point might look like this:
Relative Imports Inside a Package
Relative imports can be useful for closely related modules, but they must run inside package context.
Both absolute and relative styles are valid. Teams usually pick one convention to keep reviews consistent.
Avoid sys.path Hacks
You can mutate sys.path to force imports, but this hides packaging problems and makes behavior environment-dependent.
Prefer one of these instead:
- Install the project in editable mode with
pip install -e . - Run modules with
python -m package.module - Keep source under a package directory and test from project root
These patterns make IDE, test runner, and production behavior consistent.
Debugging Import Errors Quickly
When an import fails, print runtime clues.
Then confirm the package is installed or discoverable from the current working directory. In virtual environments, verify the active interpreter is the one where dependencies and your package are installed.
Keep Imports Stable in Tests and Tooling
Test runners and task tools may change working directory behavior. Add a simple smoke test that imports your package exactly the way production code does. This catches path drift early.
Run tests from project root in CI so local and remote import resolution rules stay aligned.
Common Pitfalls
- Running module files directly instead of using package execution with
-m. - Missing
__init__.pyfiles in package directories. - Mixing absolute and relative imports without a team convention.
- Fixing errors with
sys.pathmutation instead of proper packaging. - Using one interpreter in terminal and another in IDE, causing inconsistent import behavior.
Summary
- Organize code as a package and prefer absolute imports for long-term clarity.
- Execute entry points with
python -mfrom project root. - Use relative imports only within clear package boundaries.
- Avoid
sys.pathworkarounds that mask structural issues. - Validate interpreter and module path settings when debugging import failures.
Related reading
- Importing module from string variable using __import__ gives different results than a normal import statement
- Importing modules from parent folder
- Importing modules from parent folder
- Importing TensorFlow fails with a SyntaxError, complaining about a parameter called async
- Importing tensorflow makes python 3.6.5 error
- Importing variables from another file?
- ImproperlyConfiguredError about app_name when using namespace in include
- Improve subplot size/spacing with many subplots
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.