[Python-ideas] Pseudo-package for current directory

Chris Angelico rosuav at gmail.com
Mon Mar 7 11:54:41 EST 2016


Every once in a while, the issue pops up again of "import X" picking
up X.py from the current directory, when a stdlib or pip-installed
module named X was wanted. Python almost has a mechanism for
protecting against this, in the form of explicit package relative
imports; the only problem is that the current directory isn't a
package.

So the solution is to treat the current directory as a pseudo-package.
It'd be a backward-incompatible change, so it would need to be
explicitly invoked. Something like:

python3 -p somefile.py

which would pretend to create an __init__.py in the current directory,
change to the parent, and "from dirname import somefile".

Is that something that would work with the current import system? It'd
offer the same protection that Unix systems have had for decades: the
current directory is not in $PATH, and if you want to run a script
that's "next door to you", you explicitly request it as ./scriptname.
Idle could be protected from accidentally importing someone's
"html.py", because "import html" shouldn't ever import from the
current directory.

ChrisA


More information about the Python-ideas mailing list