[Python-ideas] Implicit submodule imports

Erik Bray erik.m.bray at gmail.com
Thu Sep 25 02:01:46 CEST 2014


On Wed, Sep 24, 2014 at 2:14 PM, Andrew Barnert
<abarnert at yahoo.com.dmarc.invalid> wrote:
> On Sep 24, 2014, at 10:57, Thomas Gläßle <t_glaessle at gmx.de> wrote:
>
>> Hey folks,
>>
>> What do you think about making it easier to use packages by
>> automatically importing submodules on attribute access.
>
> Doesn't IPython already have this feature as an option?

I don't *think* IPython has exactly this feature.  Rather, when typing
an import statement it will check to see if there are any submodules
and add them to the autocomplete list.  So I don't think typing

>>> import foo

will automatically mean a submodule foo.bar is imported, even though
it shows up on the autocomplete list when typing the import statement.
I could be wrong about this though.

In any case unless it were a feature built into Python I think this
has potential to be highly confusing to newcomers.  They might type

>>> import scipy

and start using scipy.stats in their code.  But then when they dump
this code to a script they won't have `import scipy.stats` in script,
just `import scipy`.  Then, suddenly, when they write their script
they'll get `AttributeError: stats` and then come complaining to some
mailinglist or StackOverflow that something broke their scipy
installation ;)

> I know that not everyone who uses scipy and matplotlib uses IPython, and they aren't the only two packages used by novices that have sub modules they don't automatically import for you, but... I'm guessing the percentages are high.
>
> Of course this support could also be added to scipy and matplotlib itself.
>
> And maybe importlib could have a function that makes automatic lazy loading of submodules on demand a one-liner for packages that want to support it.

This definitely has some appeal though, and shouldn't be outside the
realm of possibility.  I especially like the suggestion of making it
optional.

Erik


More information about the Python-ideas mailing list