(Wow, I'm late to this thread.)
FTR, the underlying cause is that a script's directory is prepended to
sys.path. [1] Otherwise importing your script wouldn't be a problem
nearly as often. This is something I've hoped we could address at
some point. [2] Note that PEP 395 [3] aimed to help with a number of
related issues and PEP 432 [4] may still help make it easier to sort
out interpreter-startup-related matters like this. Personally I'm
still in favor of deprecating "sys.path[0] auto-initialisation", but
I'm not gonna hold my breath on that. :)
-eric
[1] https://hg.python.org/cpython/file/7e48300c7f3b/Modules/main.c#l251
https://hg.python.org/cpython/file/7e48300c7f3b/Lib/runpy.py#l258
[2] http://bugs.python.org/issue13475
[3] http://www.python.org/dev/peps/pep-0395/
[4] http://www.python.org/dev/peps/pep-0432/
On Fri, Jan 29, 2016 at 3:42 PM, Ned Batchelder
Hi,
A common question we get in the #python IRC channel is, "I tried importing a module, but I get an AttributeError trying to use the things it said it provided." Turns out the beginner named their own file the same as the module they were trying to use.
That is, they want to try (for example) the "azure" package. So they make a file called azure.py, and start with "import azure". The import succeeds, but it has none of the contents the documentation claims, because they have imported themselves. It's baffling, because they have used the exact statements shown in the examples, but it doesn't work.
Could we make this a more obvious failure? Is there ever a valid reason for a file to import itself? Is this situation detectable in the import machinery?
--Ned. _______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/