Content · Programming · Python

Python Absolute Imports vs Relative Imports

An absolute import begins from the importable package or top-level module; a relative import uses dots to refer to the current package and its parents. Relative imports require an appropriate package context.

2 views Published Oct 17, 2026 Updated Oct 18, 2026 Admin

This guide explains absolute imports vs relative imports through a focused Python example. You will learn the behavior to expect, why it matters in a real program, and how to recognize the common mistakes before applying it in your own project.

What You Will Learn


An absolute import begins from the importable package or top-level module; a relative import uses dots to refer to the current package and its parents. Relative imports require an appropriate package context.

When and Why to Use It


The following example focuses on a practical use of Absolute Imports vs Relative Imports.

Step-by-Step Example


# project example (separate files)
# shop/__init__.py
# shop/pricing.py  defines: def tax(amount): return amount * 0.1
# shop/orders.py   uses: from .pricing import tax

import math
print(math.floor(4.9))

Understanding the Example


The module layout explains relative syntax with one leading dot. The runnable section demonstrates an absolute import from the standard library.

Expected Output


4

Important Behavior and Edge Cases


Running a package module directly as a file can make relative imports fail. Use python -m shop.orders from the project root.

Try It Yourself


Create a two-file package and test an absolute import and a relative import.

Common Beginner Mistakes


  • Running a package module directly as a file can make relative imports fail. Use python -m shop.orders from the project root.
  • Assuming that an import has no initialization effects or that every environment resolves modules in the same way.
  • Skipping the practice step and assuming the example covers every input. Test at least one normal case and one edge case relevant to this topic.

Key Takeaways


  • An absolute import begins from the importable package or top-level module; a relative import uses dots to refer to the current package and its parents.
  • The module layout explains relative syntax with one leading dot.
  • Apply the technique deliberately, checking its documented input requirements and failure behavior before integrating it into a larger project.
Share this resourceSend this page to someone who may find it useful.