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

Guido van Rossum guido at python.org
Wed Mar 14 15:09:55 EDT 2018


Yeah, one should never add a module to sys.path that has a __init__.py file.

On Wed, Mar 14, 2018 at 11:25 AM, Brendan Barnwell <brenbarn at brenbarn.net>
wrote:

> On 2018-03-14 04:18, Steven D'Aprano wrote:
>
>> Apart from intentionally manipulating sys.modules or the import system,
>> or playing file system tricks like hard-linking your files, under what
>> circumstances can this occur by accident?
>>
>
>         It can occur if a given directory winds up appearing twice on the
> import path.  For instance, if /foo is on the path and /foo/bar is a
> package directory with /foo/bar/baz as a subpackage directory, then you can
> do "from bar import baz" and "import baz" and wind up with two different
> module objects referring to the same module.
>
>         This usually happens when code starts adding paths to sys.path.
> This is in some sense "manipulating the import system" but it's something
> that a fair number of libraries do in various contexts, in order to be able
> to do things like import plugins without requiring the user to make those
> plugins available on the default import path.
>
>         For what it's worth, I have been bitten by the problem a few
> times, although it's not very common.  I think it's worth considering the
> proposal, but not sure if any change is justified given that the issue is
> fairly obscure.
>
> --
> Brendan Barnwell
> "Do not follow where the path may lead.  Go, instead, where there is no
> path, and leave a trail."
>    --author unknown
>
> _______________________________________________
> 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/
>



-- 
--Guido van Rossum (python.org/~guido)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180314/c0d13e3c/attachment.html>


More information about the Python-ideas mailing list