How do i search for a specific module in python?

Source code: Lib/

This module provides a ModuleFinder class that can be used to determine the set of modules imported by a script. can also be run as a script, giving the filename of a Python script as its argument, after which a report of the imported modules will be printed.

modulefinder.AddPackagePath(pkg_name, path)

Record that the package named pkg_name can be found in the specified path.

modulefinder.ReplacePackage(oldname, newname)

Allows specifying that the module named oldname is in fact the package named newname.

class modulefinder.ModuleFinder(path=None, debug=0, excludes=[], replace_paths=[])

This class provides run_script() and report() methods to determine the set of modules imported by a script. path can be a list of directories to search for modules; if not specified, sys.path is used. debug sets the debugging level; higher values make the class print debugging messages about what it’s doing. excludes is a list of module names to exclude from the analysis. replace_paths is a list of (oldpath, newpath) tuples that will be replaced in module paths.


Print a report to standard output that lists the modules imported by the script and their paths, as well as modules that are missing or seem to be missing.


Analyze the contents of the pathname file, which must contain Python code.


A dictionary mapping module names to modules. See Example usage of ModuleFinder.

Example usage of ModuleFinder¶

The script that is going to get analyzed later on (

import re, itertools

    import baconhameggs
except ImportError:

    import guido.python.ham
except ImportError:

The script that will output the report of

from modulefinder import ModuleFinder

finder = ModuleFinder()

print('Loaded modules:')
for name, mod in finder.modules.items():
    print('%s: ' % name, end='')

print('Modules not imported:')

Sample output (may vary depending on the architecture):

Loaded modules:
copyreg:  _inverted_registry,_slotnames,__all__
sre_compile:  isstring,_sre,_optimize_unicode
sre_constants:  REPEAT_ONE,makedict,AT_END_LINE
re:  __module__,finditer,_expand
__main__:  re,itertools,baconhameggs
types:  __module__,IntType,TypeType
Modules not imported:

How do I find a particular module in Python?

You can manually go and check the PYTHONPATH variable contents to find the directories from where these built in modules are being imported. Running "python -v"from the command line tells you what is being imported and from where. This is useful if you want to know the location of built in modules.

How will you search path for a module?

So, to ensure that your module is found, you need to do one of the following: Put in the directory where the input script is located, or the current directory if interactive. Modify the PYTHONPATH environment variable to contain the directory where is located before starting the interpreter.

Why can't Python find a module?

This is caused by the fact that the version of Python you're running your script with is not configured to search for modules where you've installed them. This happens when you use the wrong installation of pip to install packages.

Tải thêm tài liệu liên quan đến bài viết How do i search for a specific module in python?