When or why to use relative imports in Python

Are there any rules or recommendations regarding using relative imports in Python? I see them in use all the time, like in a flash framework. When searching for this topic, I see only articles on how to use relative imports, but not why.

So there is a particular benefit of using:

from . import x 

but not:

 from package import x 

In addition, I noticed that in the question this SO, the answer mentions that relative imports are not encouraged. But people still continue to use them.

+17
python import module package
Oct 05
source share
1 answer

Check PEP 328 for relative imports.

The rationale is similar to what is written:

Several use cases were presented, the most important of which was the ability to change the structure of large packages without the need to edit subpackages. In addition, the module inside the package cannot easily import without relative import.

+16
Oct 05
source share



All Articles