Skip to content

Suggest the possible module in ModuleNotFoundError #134175

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

  1. Locked-chess-official commented on May 18, 2025

    @Locked-chess-official
    ContributorAuthor

    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.

  2. added
    interpreter-core(Objects, Python, Grammar, and Parser dirs)
    stdlibStandard Library Python modules in the Lib/ directory
    and removed
    interpreter-core(Objects, Python, Grammar, and Parser dirs)
    on May 18, 2025
  3. picnixz commented on May 18, 2025

    @picnixz
    Member
  4. StanFromIreland commented on May 18, 2025

    @StanFromIreland
    Member

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

  5. 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.

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

    @StanFromIreland
    Member

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

    That too:-)

  8. Locked-chess-official commented on May 18, 2025

    @Locked-chess-official
    ContributorAuthor

    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.

  9. Locked-chess-official commented on May 18, 2025

    @Locked-chess-official
    ContributorAuthor

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

  10. Locked-chess-official commented on May 18, 2025

    @Locked-chess-official
    ContributorAuthor

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

  11. Locked-chess-official commented on May 18, 2025

    @Locked-chess-official
    ContributorAuthor

    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.

  12. 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.

  13. added
    topic-replRelated to the interactive shell
    and removed
    pendingThe issue will be closed if no feedback is provided
    on May 18, 2025
  14. added
    interpreter-core(Objects, Python, Grammar, and Parser dirs)
    and removed
    stdlibStandard Library Python modules in the Lib/ directory
    topic-replRelated to the interactive shell
    on Aug 9, 2025
  15. picnixz commented on Aug 9, 2025

    @picnixz
    Member

    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.

  16. Locked-chess-official commented on Aug 9, 2025

    @Locked-chess-official
    ContributorAuthor

    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.

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

    interpreter-core(Objects, Python, Grammar, and Parser dirs)type-featureA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions