Skip to content

Files

Latest commit

 

History

History
28 lines (17 loc) · 945 Bytes

relative-import.md

File metadata and controls

28 lines (17 loc) · 945 Bytes

Pattern: Use of relative import instead of absolute import

Issue: -

Description

This rule enforces PEP 8 style guide recommendation to use absolute over relative imports. They are usually more readable and tend to be better behaved (or at least give better error messages) if the import system is incorrectly configured (such as when a directory inside a package ends up on sys.path):

import mypkg.sibling
from mypkg import sibling
from mypkg.sibling import example

However, explicit relative imports are an acceptable alternative to absolute imports, especially when dealing with complex package layouts where using absolute imports would be unnecessarily verbose:

from . import sibling
from .sibling import example

Standard library code should avoid complex package layouts and always use absolute imports.

Further Reading