[Python-ideas] Disallow importing the same module under multiple names
Brendan Barnwell
brenbarn at brenbarn.net
Wed Mar 14 14:25:28 EDT 2018
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
More information about the Python-ideas
mailing list