Python › Modules & Packaging
What is the difference between an absolute import and a relative import within a package?
IntermediateAnswer
An absolute import specifies the full path from the top-level package (`from mypackage.utils import helper`), while a relative import uses leading dots to reference modules relative to the current module's position in the package (`from .utils import helper` for a sibling, `from ..core import Base` for a parent package). Relative imports only work within a package context (not in standalone scripts) and make internal restructuring easier since you don't hardcode the package's top-level name.
# Inside mypackage/subpkg/module.py
from . import sibling_module # relative: same package
from .. import top_level_module # relative: parent package
from mypackage.subpkg import sibling_module # absolute equivalentReal-world example A large internal package uses relative imports throughout its submodules so the entire package can be renamed or vendored into another project without rewriting every absolute import path.