On Fri, Aug 21, 2020, at 15:13, Gustav O wrote:
In the beginning of the programming journey, getting a message about circular imports when they were testing out tkinter in a file names "tkinter.py" would probably not be helpful. Getting a message that hints that you may have accidentally imported itself, however, will probably help significantly.
Maybe an error like this would be better, even though the wording could be worked on: "AttributeError: partially initialized module 'tkinter' has no attribute 'Tk' (most likely due to trying to import the file that is being executed)"
Detecting being partially loaded won't be enough for the shadowed file case, consider this minimal example of a file named tkinter.py:
if name == '__main__': root = tkinter.Tk()
This file is loaded twice, once as __main__ and once as tkinter. The one that is loaded as tkinter and imported is *not* partially initialized, it finishes initializing with no errors.
It may be necessary to do more introspection (does the import machinery have a way to see if a module name exists twice on the path?) to determine when an AttributeError is caused by shadowing. It can also happen deep inside stdlib code, if one module whose name is not shadowed imports a shadowed module.