Pseudo-package for current directory
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
participants (9)
-
Brett Cannon
-
Chris Angelico
-
Joseph Martinot-Lagarde
-
Nick Coghlan
-
Niki Spahiev
-
Random832
-
Rob Cliffe
-
Steven D'Aprano
-
Terry Reedy