September 11, 2024

Checking Installed Modules in Python

To check the installed modules in Python, you can use one of the following methods:

  1. Using pip list in the terminal or command prompt:
    pip list

    This command will list all the installed packages along with their versions.

  2. Using pip freeze in the terminal or command prompt:
    pip freeze

    This command will list all the installed packages in a format suitable for requirements.txt files.

  3. Using Python code to check installed modules:
    import pkg_resources
    
    # List all installed packages
    installed_packages = pkg_resources.working_set
    installed_packages_list = sorted(["%s==%s" % (pkg.key, pkg.version) for pkg in installed_packages])
    for package in installed_packages_list:
        print(package)

    This code snippet uses the pkg_resources module to list installed packages along with their versions.

Choose the method that best fits your needs. The pip list and pip freeze commands are usually the most straightforward options for quickly checking installed modules.