absolute import
An absolute import specifies a module using its fully qualified name, for example: package.subpackage.module. Because it doesn’t depend on the importing module’s location, an absolute import is typically unambiguous and often preferred for readability and maintainability, in line with PEP 8’s guidance.
When you use an absolute import, Python resolves the name starting at the top-level package. That means the directory containing your project’s top-level package must be on import path.
Example
Suppose you have the following project layout:
project/
└── math_pkg/
├── __init__.py
├── report.py
└── statistics.py
Create a function in statistics.py:
math_pkg/statistics.py
def mean(values):
return sum(values) / len(values)
Use an absolute import in report.py:
math_pkg/report.py
from math_pkg.statistics import mean
def main():
print("mean:", mean([1, 2, 3]))
if __name__ == "__main__":
main()
Here, from math_pkg.statistics import mean is an absolute import that starts at the top-level package math_pkg. The equivalent relative import inside math_pkg would be from .statistics import mean, but absolute imports remain clear regardless of a module’s location.
Run the module from the project directory with -m so the top-level package is on sys.path:
$ cd project
$ python -m math_pkg.report
mean: 2.0
Related Resources
Tutorial
Absolute vs Relative Imports in Python
If you’ve worked on a Python project that has more than one file, chances are you’ve had to use an import statement before. In this tutorial, you’ll not only cover the pros and cons of absolute and relative imports but also learn about the best practices for writing import statements.
For additional information on related topics, take a look at the following resources:
By Leodanis Pozo Ramos • Updated Dec. 23, 2025