What's the rationale behind not allowing * in relative imports? e.g.
from ..new_tool import *
or doing a relative import directly:
import ..new_tool
Because there, they want to make sure that other developers could also get the full path of the import module. Relative imports are helpful when you are working alone on a Python project, or the module is in the same directory where you are importing the module.
Relative imports use dot(.) notation to specify a location. A single dot specifies that the module is in the current directory, two dots indicate that the module is in its parent directory of the current location and three dots indicate that it is in the grandparent directory and so on.
Note that relative imports are based on the name of the current module. Since the name of the main module is always “main”, modules intended for use as the main module of a Python application must always use absolute imports.
The reason the latter is prohibited is that ..new_tool
is not usable in an expression (PEP 328):
The reason
import .foo
is prohibited is because afterimport XXX.YYY.ZZZ
then
XXX.YYY.ZZZ
is usable in an expression. But.moduleY
is not usable in an expression.
Since *
-imports should only ever be a quick hack while in development, I suspect the functionality for relative *
-imports was left out because it's not necessary.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With