GeezerVison is showing github.com — Suggest the possible module in ModuleNotFoundError · Issue #134175 · python/cpython
Suggest the possible module in ModuleNotFoundError #134175DescriptionFeature or enhancement
Proposal:
When import the module, it will raise ModuleNotFoundError if the word is wrong. Like NameError, we can suggest the module name that might be right.
>>>import ant
Traceback (most recent call last):
File <stdin> line 1, in <module>
import ant
^^^
ModuleNotFoundError: no module named 'ant'. Did you mean 'ast'?
To get all of the module, this function can get:
import pkgutil
def get_available_modules():
return [name for _, name, _ in pkgutil.iter_modules()]
available_modules = get_available_modules()
Just need to compare the most likely module name and suggest.
Note: only when the ModuleNotFoundError was raised by python interpreter instead of raise active
Has this already been discussed elsewhere?
No response given
Links to previous discussion of this feature:
No response ActivityLocked-chess-official commented on May 18, 2025 ContributorAuthor More actions Ok. Maybe some module cannot be imported by the current interpreter. If the Exception raise by this (name is in the "available_modules") maybe we need to suggest another message. picnixz commented on May 18, 2025 StanFromIreland commented on May 18, 2025 It won't be as easy, importlib is frozen, pkgutil is not. IIRC it only had one dependency, sys. picnixz commented on May 18, 2025 It's not about being frozen, it's more about the fact that it can be very slow to iterate over millions of installed packages just to find a match as the namespace to look in is the "entire" site-packages in this case. StanFromIreland commented on May 18, 2025
it's more about the fact that it can be very slow to iterate over millions of installed packages
That too:-) Locked-chess-official commented on May 18, 2025 ContributorAuthor More actions I think that we can distinguish the grade to compare.
First is the builtin-module.
Second is the standard module.('Lib')
Third is the project module.
Finally is the site-packages.('Lib/site-package')
It can solve some of the speed problem.
It's not about being frozen, it's more about the fact that it can be very slow to iterate over millions of installed packages just to find a match as the namespace to look in is the "entire" site-packages in this case.
Locked-chess-official commented on May 18, 2025 ContributorAuthor More actions And we don't need to compare for the pypi packages that wasn't installed. Locked-chess-official commented on May 18, 2025 ContributorAuthor More actions If there is too many site-packages, the solution might be create the virtual environments. Locked-chess-official commented on May 18, 2025 ContributorAuthor More actions
It's not about being frozen, it's more about the fact that it can be very slow to iterate over millions of installed packages just to find a match as the namespace to look in is the "entire" site-packages in this case.
And if that's True, the size of the python might be between 1T to 12T, whith means that the computer cannot run any other things, can only be as a docker, I think. picnixz commented on May 18, 2025 Actually, we could perhaps use #129329. However, it appears that we're building a global cache anyway, so it may not be impossible after all.
In addition, now that I think about, we should probably only need to present suggestions for top-level packages and leave the rest to attribute suggestion when possible. picnixz commented on Aug 9, 2025 I'm closing this one because the cache needs to be built at the interpreter's level which is not a good idea due to resources managements and references being held. The REPL autocompletion works well because the cache is at the REPL level but suggestions are constructed in C. Locked-chess-official commented on Aug 9, 2025 ContributorAuthor More actions
I'm closing this one because the cache needs to be built at the interpreter's level which is not a good idea due to resources managements and references being held. The REPL autocompletion works well because the cache is at the REPL level but suggestions are constructed in C.
Ok. The ideal is based on that 'it need a cache'. Well, the pkgutil don't need to import any packages, just analyses and it is fast. What you are worried is that "if there is too many packages". However, in my computer, there are only hundreds of packages and it is still fast, while 2/3 of the storage space of the computer has been occured(alomost 600/900gb), so the normally computer cannot install millions of packages. In this case, I'm suspected that whether the cache is needed.
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|