-
-
Notifications
You must be signed in to change notification settings - Fork 30.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add more specific error message when file has same name as an imported module #95754
Comments
Hi @janine9vn 👋🏼 What version are you using? I tried this on version 3.9.13 and getting a slightly different phrased error
I agree, ImportError class would be better suited. Perhaps a better error message could be:
Open to suggestions, would love to take this up once finalised! |
Thanks for opening this issue! I agree that we could likely make some changes to make this common situation easier to debug, especially for people new to Python. I don't think that we should change the exception type from Fortunately, I think the exception type is not the most important thing here; I think we improve debuggability much more by changing the exception message itself. One simple change that I think would be worthwhile would be to add the file path to the module to the error message, so it would be more obvious that the
There's more that we could consider, but I think even this much would be a pretty good start. |
Consider using |
) Co-authored-by: Serhiy Storchaka <[email protected]>
We've implemented carljm's suggestion. Are there any other concrete suggestions on how to improve the error? We could try special casing standard library here and provide a different error message if the module name is in |
…python#112577) Co-authored-by: Serhiy Storchaka <[email protected]>
…python#112577) Co-authored-by: Serhiy Storchaka <[email protected]>
…python#112577) Co-authored-by: Serhiy Storchaka <[email protected]>
Okay, I implemented a more thorough set of new error messages in #113769 |
…python#112577) Co-authored-by: Serhiy Storchaka <[email protected]>
Feature / enhancement
Currently, if a python file I create and am trying to run has the same name as a module I'm importing in it (i.e. my file is named
random.py
and I try to doimport random; print(random.randint(5))
, the following error message appears:Instead, for this particular scenario, a more specific error message would be helpful:
Pitch
Frequently, someone learning python will try to name their python files after the topic they are learning about. These topics can be about a specific module (e.g. random, turtle, pygame, requests). For instance, someone experimenting with the
turtle
module, might name their python fileturtle.py
. This results in the scenario of a person trying to import a module, but instead their program tries to import itself because of the name collision.The current error message isn't clear that the issue is the filename conflicting with the module name and overriding it. Folks with more experience can deduce that from the "circular import" portion, but beginners are often confused by the "AttributeError" portion. I think this scenario would be a good candidate for a more specific error message, to better warn about this common pitfall.
The recent improvements for more detailed and specific error messages have helped many people, especially those new to Python. I think this would be a worthwhile addition in that same vein.
Considerations & discussion points
The example improved error message I have above is very much open to improvements, if folks have suggestions.
I am also operating under the somewhat informed assumption that there isn't a valid case for a file to import itself, so there would not be an issue with this error appearing if the imported module is the same name as the file itself.
Previous discussion
Linked PRs
The text was updated successfully, but these errors were encountered: