The error ImportError: attempted relative import with no known parent package in Python confuses many developers. It happens when you run a file that uses relative imports as a script instead of as part of a package. Here’s why it happens and how to fix it.
๐ Table of Contents
- Why This Happens
- The Setup That Triggers It
- Fix 1: Run as a Module with -m
- Fix 2: Use Absolute Imports
- Fix 3: Add a Proper Entry Point
- Fix 4: Make It a Proper Installable Package
- Understanding Relative vs Absolute Imports
- The __init__.py Requirement
- Quick Decision Guide
- Frequently Asked Questions
- Conclusion
Why This Happens
Relative imports (from .module import x) only work when Python knows the file is part of a package. When you run a file directly (python myfile.py), Python treats it as a top-level script with no parent package โ so relative imports fail. The . in the import has no package to be relative to.
The Setup That Triggers It
# Project structure
myproject/
โโโ mypackage/
โ โโโ __init__.py
โ โโโ main.py
โ โโโ helper.py
# main.py uses a relative import
# from .helper import do_something
# ๐ Running directly fails
python mypackage/main.py
# ImportError: attempted relative import with no known parent package
Fix 1: Run as a Module with -m
# Run from the PROJECT ROOT (parent of mypackage) using -m
cd myproject
python -m mypackage.main
# The -m flag runs it as a module within the package,
# so Python knows the parent package and relative imports work
This is the correct way to run package code that uses relative imports. Note you run from the project root and use dots, not slashes.
Fix 2: Use Absolute Imports
# Instead of relative imports:
# from .helper import do_something # relative
# Use absolute imports:
from mypackage.helper import do_something # absolute
# Absolute imports work whether run as a script or module,
# as long as the package is on the Python path
Fix 3: Add a Proper Entry Point
# Create a run script at the project root (outside the package)
# myproject/run.py
from mypackage.main import main
if __name__ == '__main__':
main()
# Run it from the root โ absolute imports resolve correctly
python run.py
Fix 4: Make It a Proper Installable Package
# pyproject.toml
[project]
name = "mypackage"
version = "0.1.0"
[project.scripts]
mypackage = "mypackage.main:main"
# Install in editable mode
pip install -e .
# Now run via the entry point โ imports work correctly
mypackage
# Or: python -m mypackage.main
Understanding Relative vs Absolute Imports
# Relative imports (need a parent package)
from . import sibling_module # same package
from .helper import func # module in same package
from ..other_package import thing # parent package
# Absolute imports (need package on path)
from mypackage.helper import func
from mypackage.subpackage.mod import thing
# Rule of thumb: absolute imports are more explicit and robust.
# Use them unless you have a specific reason for relative imports.
The __init__.py Requirement
# For a directory to be a package (enabling relative imports),
# it typically needs an __init__.py file:
mypackage/
โโโ __init__.py # makes mypackage a package (can be empty)
โโโ main.py
โโโ helper.py
# Without __init__.py, Python may treat it as a namespace package
# (works in modern Python but can cause confusion). For clarity,
# include __init__.py in packages that use relative imports.
Quick Decision Guide
- Running a package file for development? Use
python -m package.modulefrom the project root - Want imports that always work? Use absolute imports (
from mypackage.x import y) - Building a real project? Make it an installable package with an entry point
- Just a simple script? Don’t use relative imports โ use absolute or put everything in one file
Frequently Asked Questions
Q: Why do relative imports work in some files but not others?
A: They work when the file is imported as part of a package, but fail when run directly as a script. The same file can work when imported and fail when run with python file.py โ because direct execution has no parent package.
Q: Should I use relative or absolute imports?
A: Absolute imports are generally recommended โ they’re explicit, work in more situations, and are clearer about where things come from. Use relative imports only within a package when it improves readability, and understand they require running as a module.
Q: What does the -m flag actually do?
A: It runs a module as part of its package, setting up the package context so relative imports resolve. python -m mypackage.main tells Python “run main as a module inside mypackage,” giving it the parent package it needs.
Q: Do I always need __init__.py?
A: In modern Python, namespace packages work without it, but including __init__.py makes your package explicit and avoids ambiguity, especially when using relative imports. It’s good practice to include it.
Q: My IDE runs it fine but the terminal fails. Why?
A: IDEs often set the working directory and Python path to the project root and may run files as modules. Replicate that in the terminal: run from the project root with python -m package.module, or configure your path/entry point to match.
Conclusion
“Attempted relative import with no known parent package” means you’re running a package file directly as a script, so relative imports have no parent package to resolve against. The fixes: run it as a module with python -m mypackage.main from the project root, switch to absolute imports (from mypackage.helper import x), or create a proper entry point at the project root. For robust projects, make it an installable package with an entry point. In general, prefer absolute imports โ they’re explicit and work whether your code runs as a script or a module.
๐ You might also like
๐ Share this article




โ๏ธ Leave a Comment