On Fri, Aug 21, 2020 at 11:40 PM Gustav O <[email protected]> wrote:
>
> If a file named "tkinter.py" is created and tkinter is imported and used
> within the file, the following exception is raised:
> "AttributeError: partially initialized module 'tkinter' has no attribute 'Tk'
> (most likely due to a circular import)"
>
> I've spoken to multiple beginners who got stuck on this and just couldn't
> figure out what was happening. Just like the error message tells us, it's due
> to a "circular import". In other words, we're trying to import the very file
> that is doing the import. To make the message easier to understand, I propose
> that we change the message to something that mentions that the program is
> trying to import itself.
>
> I think the following would be an option:
> "ImportError: partially initialized module 'tkinter' can't import itself"
>
> I can't think of any reason to import itself in a way like this, but even if
> there is a valid reason, the error message could probably be improved. Of
> course, this applies to more than just tkinter.
>
The trouble is, a circular import might not be the module directly
importing itself. If you create a file to try to do some arithmetic
and call it "math.py", and in there, you "import statistics", you'll
end up with a circular import:
Traceback (most recent call last):
File "/home/rosuav/tmp/aaaa/math.py", line 1, in <module>
import statistics
File "/usr/local/lib/python3.10/statistics.py", line 105, in <module>
import random
File "/usr/local/lib/python3.10/random.py", line 49, in <module>
from math import log as _log, exp as _exp, pi as _pi, e as _e, ceil as _ceil
ImportError: cannot import name 'log' from 'math'
(/home/rosuav/tmp/aaaa/math.py)
The current behaviour, as you see here, is to show the source of the
failing module. Since the "math" module normally should be a built-in,
there's a strong hint that this is where the problem is. It's more
flexible than simply catching that a module is trying to import
itself.
By the way, this actually works:
x = 1
from __main__ import x as y
print(x, y)
Is this the most ridiculous and convoluted way to write "y = x"?
ChrisA
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/E5KY2SBVYIDTQGELNKDB53Z4MWMDXP5U/
Code of Conduct: http://python.org/psf/codeofconduct/