[Python-ideas] Disallow importing the same module under multiple names

Chris Angelico rosuav at gmail.com
Wed Mar 14 02:06:02 EDT 2018


On Wed, Mar 14, 2018 at 4:58 PM, Steven D'Aprano <steve at pearwood.info> wrote:
> On Wed, Mar 14, 2018 at 04:20:20PM +1100, Chris Billington wrote:
>
>> Instead, maybe a user should just get a big fat error if they try to import
>> the same file twice under different names.
>
> Absolutely not.
>
> Suppose I import a library, Spam, which does "import numpy".
>
> Now I try to "import numpy as np", and I get an error.

That's not the same thing. Both of those statements are importing the
same file under the same name, "numpy"; one of them then assigns that
to a different local name. But in sys.modules, they're the exact same
thing.

The double import problem comes when the same file gets imported under
two different names *in sys.modules*. Everything else isn't a problem,
because you get the same module object - if you "import numpy; import
numpy as np; assert np is numpy", you're not seeing a double import
problem.

ChrisA


More information about the Python-ideas mailing list