Python – from . import

There is a PEP for everything.

Quote from PEP8: Imports

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:

Guido’s decision in PEP328 Imports: Multi-Line and Absolute/Relative

Copy Pasta from PEP328

Here’s a sample package layout:


Assuming that the current file is either or subpackage1/ , the following are all correct usages of the new syntax:

from .moduleY import spam
from .moduleY import spam as ham
from . import moduleY
from ..subpackage1 import moduleY
from ..subpackage2.moduleZ import eggs
from ..moduleA import foo
from ...package import bar
from ...sys import path

Leave a Comment