8

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
Machavity
  • 30,841
  • 27
  • 92
  • 100
Claudiu
  • 224,032
  • 165
  • 485
  • 680
  • from ..new_tool import * is not healthy.. It will import every thing under ..new_tool. It may create a conflict with your method_name or attribute_name if your declared name matches with the imported name. Some time cyclic dependencies may occur .. – Tauquir Jun 06 '11 at 21:16
  • @Tauquir: that applies to any `import *`, not just relative ones specifically. – Claudiu Jun 07 '11 at 13:17
  • still waiting for an answer on the `import *` part that doesn't also apply to the regular one – Claudiu Jun 07 '11 at 20:25

1 Answers1

7

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.

Katriel
  • 120,462
  • 19
  • 136
  • 170
  • hmm i suppose they could have done `import .foo as _foo`. it is nice not having `import *`s in your code though. – Claudiu Jun 07 '11 at 13:22
  • Be weird to allow `import ... as` but not a bare `import` though. – Katriel Jun 07 '11 at 13:43
  • true. actually i guess you can always do `from . import foo`, so you dont lose anything by not being able to do `import .foo`. – Claudiu Jun 07 '11 at 13:57