[Python-ideas] Allow 'import star' with namespaces

dag.odenhall at gmail.com dag.odenhall at gmail.com
Fri May 6 22:59:05 CEST 2011


On 6 May 2011 22:12, Ethan Furman <ethan at stoneleaf.us> wrote:
> Brendan Moloney wrote:
>>
>> dag.odenhall at gmail.com wrote:
>>>
>>> I like this idea, except it's inconsistent with from-import-star, the
>>> latter which does *not* get you sub-packages or modules.
>>
>> Georg Brandl [g.brandl at gmx.net] wrote:
>>>
>>> And that's for a reason: it's not easy (I think it's even impossible,
>>> because
>>> for example individual submodules can change __path__) to determine all
>>> importable submodules of a package.
>>
>>> So ``import pkg.*`` would not have any behavior other than ``import
>>> pkg``.
>>
>> When I said all _public_ sub-packages and modules I was referring to those
>
>> listed in the  __all__ attribute of 'pkg'.  Thus it would behave in the
>> exact
>> same way as from-import-star except you don't pollute the current
>> namespace.

If you're going to require listing in __all__ anyway, you might as
well use what already works: import the modules in the package, and
you can then import the package and access the modules as attributes:

pkg/__init__.py:
from . import mod

script.py:
import pkg
pkg.mod  #=> pkg/mod.py



More information about the Python-ideas mailing list