[Python-ideas] Prevent importing yourself?
Ned Batchelder
ned at nedbatchelder.com
Fri Jan 29 17:42:18 EST 2016
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.
More information about the Python-ideas
mailing list