Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python: What's the rationale behind not allowing * in relative imports?

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
like image 389
Claudiu Avatar asked Jun 06 '11 21:06

Claudiu


People also ask

Should we use relative imports in Python?

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.

How do you add relative imports in Python?

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.

Should I use relative or absolute imports Python?

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.


1 Answers

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 after

    import 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.

like image 137
Katriel Avatar answered Oct 05 '22 17:10

Katriel