GeezerVison is showing github.com — Getting ImportError and suggestion to rename module for no valid reason · Issue #136094 · python/cpython
Getting ImportError and suggestion to rename module for no valid reason #136094Labels interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error DescriptionBug report
Bug description:
I can't trigger the classic "partially initialized module" ImportError anymore.
Circular imports are a different issue and not related to shadowing one of sys.stdlib_module_names for example.
# mainmod.py
from secondmod import secondaryitem
mainitem = 1
# secondmod.py
from mainmod import mainitem
secondaryitem = 2
I made sure there is no package installed providing mainmod or secondmod.
In Python 3.13.5:
# python3 mainmod.py
Traceback (most recent call last):
File "/home/as/mainmod.py", line 1, in <module>
from secondmod import secondaryitem
File "/home/as/secondmod.py", line 1, in <module>
from mainmod import mainitem
File "/home/as/mainmod.py", line 1, in <module>
from secondmod import secondaryitem
ImportError: cannot import name 'secondaryitem' from 'secondmod' (consider renaming '/home/as/secondmod.py' if it has the same name as a library you intended to import)
Before in Python 3.12.6:
python3 mainmod.py
Traceback (most recent call last):
File "/home/as/mainmod.py", line 1, in <module>
from secondmod import secondaryitem
File "/home/as/secondmod.py", line 1, in <module>
from mainmod import mainitem
File "/home/as/mainmod.py", line 1, in <module>
from secondmod import secondaryitem
ImportError: cannot import name 'secondaryitem' from partially initialized module 'secondmod' (most likely due to a circular import) (/home/as/secondmod.py)
Because it's an improvement mostly for beginners, the warning should appear only when colliding with sys.stdlib_module_names or maybe other installed third party packages. It should not trigger for modules of the same project.
I've read the threads about this new Python 3.13 feature and could not find any report on this false positive warning.
CPython versions tested on:
3.13
Operating systems tested on:
Linux Activityhugovk commented on Jun 29, 2025 sausix commented on Jun 29, 2025 I know the difference between Python and CPython.
And it's an issue/change in CPython. It's not about my own code. hugovk commented on Jun 29, 2025 brianschubert commented on Jun 29, 2025 For reference, the "consider renaming" error was introduced by #113769 hauntsaninja commented on Jun 29, 2025 Thanks for the issue!
There are actually two separate warnings we issue, one for the standard library case and one for the potential third party case:
-
The standard library one checks against sys.stdlib_module_names as you suggest, and the error message is something like: AttributeError: module 'random' has no attribute 'randint' (consider renaming '/home/me/random.py' since it has the same name as the standard library module named 'random' and prevents importing that standard library module)
-
The potential third party case message is something like: AttributeError: module 'numpy' has no attribute 'array' (consider renaming '/home/me/numpy.py' if it has the same name as a library you intended to import)
Two notes:
Here are the things I'm solving for:
- I would like to issue some "consider renaming" message in the third party shadowing case
- In my experience / from forums, it is basically as common for beginners to have
numpy.py or requests.py or openai.py as it is for them to have random.py, and PyPI is only becoming more and more useful
- I don't think it is tenable to scan
site.getsitepackages (or try to use importlib.metadata.distributions or similar). This can be really slow. Also recursive imports while handling import errors can lead to some really cursed situations that can cause the interpreter to crash
Are there tweaks that would have made you less confused? For instance:
- We can restore the error message to lead with "partially initialized module" instead of "module"
- We can lead with the "if" clause, i.e. "If ... has the same name as a library you intended to import, consider renaming ..." instead of "Consider renaming ... if ...."
hauntsaninja commented on Apr 10, 2026 Closing, since OP never reacted to my suggestions, and we haven't seen additional cases of confusion sausix commented on Apr 10, 2026 I reported a bug. Clearly a regression.
Then this issue has been automatically closed for reason "Everybody is unaware between the difference of Python and CPython".
I had to register on another site to open a discussion for reopening this issue.
Now this issue is on me? I've done enough so far. It's not my job to find a solution for you on a simple issue.
Yeah just close it if you prefer better statistics than solving issues.
MetadataMetadatainterpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error DevelopmentNo branches or pull requestsIssue actions
|