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

Chris Billington chrisjbillington at gmail.com
Wed Mar 14 02:20:41 EDT 2018


Exactly.

The example in the "if __name__ == '__main__'" block of my module I linked
imports numpy as np, and then adds the numpy package directory to sys.path
and imports linalg. This is detected as numpy.linalg being imported twice,
once as numpy.linalg, and once as linalg. The output error is below.

As for "why should we do this", well, it helps prevent bugs that are pretty
hard to notice and debug, so that's a plus. I can't think of any other
pluses, so it's down to thinking  of minuses in order to see if it's a good
idea on-net.


Traceback (most recent call last):
  File "double_import_denier.py", line 153, in _raise_error
    exec('raise RuntimeError(msg) from None')
  File "<string>", line 1, in <module>
RuntimeError: Double import! The same file has been imported under two
different names,
resulting in two copies of the module. This is almost certainly a mistake.
If you are
running a script from within a package and want to import another submodule
of that package,
import it by its full path: 'import module.submodule' instead of just
'import submodule.'

Path imported: /home/bilbo/anaconda3/lib/python3.6/site-packages/numpy/
linalg

Traceback (first time imported, as numpy.linalg):
------------
  File "double_import_denier.py", line 195, in <module>
    test1()
  File "double_import_denier.py", line 185, in test1
    import numpy as np
  File "/home/bilbo/anaconda3/lib/python3.6/site-packages/numpy/__init__.py",
line 142, in <module>
    from . import add_newdocs
  File "/home/bilbo/anaconda3/lib/python3.6/site-packages/numpy/add_newdocs.py",
line 13, in <module>
    from numpy.lib import add_newdoc
  File "/home/bilbo/anaconda3/lib/python3.6/site-packages/numpy/lib/__init__.py",
line 19, in <module>
    from .polynomial import *
  File "/home/bilbo/anaconda3/lib/python3.6/site-packages/numpy/lib/polynomial.py",
line 20, in <module>
    from numpy.linalg import eigvals, lstsq, inv
------------

Traceback (second time imported, as linalg):
------------
  File "double_import_denier.py", line 195, in <module>
    test1()
  File "double_import_denier.py", line 188, in test1
    import linalg
------------

On Wed, Mar 14, 2018 at 5:06 PM, Chris Angelico <rosuav at gmail.com> wrote:

> 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
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180314/d5a3af71/attachment.html>


More information about the Python-ideas mailing list