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

Chris Billington chrisjbillington at gmail.com
Tue Mar 20 06:23:12 EDT 2018


On Tue, Mar 20, 2018 at 8:06 PM, Steven D'Aprano <steve at pearwood.info>
wrote:

> On Wed, Mar 14, 2018 at 12:09:55PM -0700, Guido van Rossum wrote:
>
> > Yeah, one should never add a module to sys.path that has a __init__.py
> file.
>
> Should import raise a warning in that case?
>
> I wouldn't want an outright error. I've cd'ed into a package directory
> in the shell, then run a python module from that directory too many
> times to want an exception, but I wouldn't mind a warning.
>
>
Not to mention that plenty of programs are designed to run in whatever
working directory they find themselves in, and that working directory may
contain __init__.py files. For example, I wonder how mercurial gets around
the fact that its own imports might be shadowed by whatever's in the
current working directory. The mercurial project uses itself for version
control, so it is presumably running with its working directory somewhere
in its own source tree all the time. I wonder if mercurial removes the
current working directory from sys.path to avoid any problems. A lot of
programming tools no doubt often find themselves in working directories
that are python packages.

A warning would be pretty good! Especially if you could flip a switch to
turn it into an error. Not if there is merely an __init__.py in view, but
if you actually do an import twice, since a lot of code (with fully
qualified imports, no submodules with names shadowing stdlib modules, etc)
would never hit a problem with running from the package directory. It seems
like running from within a package directory is bad news mostly *because*
of the double import problem, and would be somewhat less of a bad idea if
you could be confident you didn't have any accidental double imports (still
something of a bad idea though because you can't know that your submodule
isn't shadowing some other 3rd party module indirectly imported by your
code, but that's about the only remaining issue with it I can think of).

-Chris



> --
> Steve
> _______________________________________________
> 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/20180320/059fc919/attachment.html>


More information about the Python-ideas mailing list