[Python-ideas] Re: Improve error message for trying to import itself

2020-08-21 Thread Steven D'Aprano
On Fri, Aug 21, 2020 at 09:35:14AM -, Gustav O 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

[Python-ideas] Re: Improve error message for trying to import itself

2020-08-21 Thread Greg Ewing
On 22/08/20 6:43 am, Serhiy Storchaka wrote: It would have non-zero cost. There is a common idiom: try: from foo import bar except ImportError: def bar(): ... In this case you would need to try importing foo from other locations. I wouldn't suggest going that far.

[Python-ideas] Re: Improve error message for trying to import itself

2020-08-21 Thread Random832
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

[Python-ideas] Re: Improve error message for trying to import itself

2020-08-21 Thread Gustav O
I actually didn't know that there were use cases for importing itself — thanks for letting me know. The best way of implementing a better error message would probably be to change the message for when the user import itself, while it's still partially initialized. In the beginning of the

[Python-ideas] Re: Improve error message for trying to import itself

2020-08-21 Thread Serhiy Storchaka
21.08.20 18:22, Greg Ewing пише: > Maybe check whether the module being imported from is shadowing another > module further along the search path and warn about that? It would have non-zero cost. There is a common idiom: try: from foo import bar except ImportError: def

[Python-ideas] Re: Improve error message for trying to import itself

2020-08-21 Thread Serhiy Storchaka
21.08.20 12:35, Gustav O пише: > 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

[Python-ideas] Re: Improve error message for trying to import itself

2020-08-21 Thread Gustav O
Great that you point that out. I have thought of are two possible solutions to this: 1. Make the current error message clearer, but make sure that it's correct for both cases 2. Create a special case for trying to import itself. I personally think the second option would be superior. If

[Python-ideas] Re: Improve error message for trying to import itself

2020-08-21 Thread Greg Ewing
Maybe check whether the module being imported from is shadowing another module further along the search path and warn about that? -- Greg ___ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to

[Python-ideas] Re: Improve error message for trying to import itself

2020-08-21 Thread Chris Angelico
On Fri, Aug 21, 2020 at 11:40 PM Gustav O 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

[Python-ideas] Improve error message for trying to import itself

2020-08-21 Thread Gustav O
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