Skip to content

Suggest the possible module in ModuleNotFoundError #134175

Open
@Locked-chess-official

Description

@Locked-chess-official

Feature 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

Activity

Locked-chess-official

Locked-chess-official commented on May 18, 2025

@Locked-chess-official
Author

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.

added
interpreter-core(Objects, Python, Grammar, and Parser dirs)
stdlibPython modules in the Lib dir
and removed
interpreter-core(Objects, Python, Grammar, and Parser dirs)
on May 18, 2025
picnixz

picnixz commented on May 18, 2025

@picnixz
Member
StanFromIreland

StanFromIreland commented on May 18, 2025

@StanFromIreland
Contributor

It won't be as easy, importlib is frozen, pkgutil is not. IIRC it only had one dependency, sys.

picnixz

picnixz commented on May 18, 2025

@picnixz
Member

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.

added
pendingThe issue will be closed if no feedback is provided
on May 18, 2025
StanFromIreland

StanFromIreland commented on May 18, 2025

@StanFromIreland
Contributor

it's more about the fact that it can be very slow to iterate over millions of installed packages

That too:-)

Locked-chess-official

Locked-chess-official commented on May 18, 2025

@Locked-chess-official
Author

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

Locked-chess-official commented on May 18, 2025

@Locked-chess-official
Author

And we don't need to compare for the pypi packages that wasn't installed.

Locked-chess-official

Locked-chess-official commented on May 18, 2025

@Locked-chess-official
Author

If there is too many site-packages, the solution might be create the virtual environments.

Locked-chess-official

Locked-chess-official commented on May 18, 2025

@Locked-chess-official
Author

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

picnixz commented on May 18, 2025

@picnixz
Member

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.

3 remaining items

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibPython modules in the Lib dirtopic-replRelated to the interactive shelltype-featureA feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Suggest the possible module in ModuleNotFoundError · Issue #134175 · python/cpython

      Follow Lee on X/Twitter - Father, Husband, Serial builder creating AI, crypto, games & web tools. We are friends :) AI Will Come To Life!

      Check out: eBank.nz (Art Generator) | Netwrck.com (AI Tools) | Text-Generator.io (AI API) | BitBank.nz (Crypto AI) | ReadingTime (Kids Reading) | RewordGame | BigMultiplayerChess | WebFiddle | How.nz | Helix AI Assistant