How to list installed Python modules locally?

How do I get a python list modules that are installed on my computer?

How can I retrieve a list of Python modules installed locally on my system?

Hey, I would suggest starting with the pip list command. It’s a simple and effective way to get a list of all installed Python modules. Here’s how you can use it:

import subprocess

result = subprocess.run(["pip", "list"], capture_output=True, text=True)
print(result.stdout)

This will print out all installed Python modules and their versions. If you prefer, you can also run pip list directly from your terminal without the need for Python code. It’s pretty handy for quick checks!

Thank you

Good point! But if you’re looking for something more programmatic, you might want to try the pkg_resources module. It allows you to retrieve the list of installed modules directly within your Python code, and you can easily manipulate the results as needed. Here’s how:

import pkg_resources

installed_packages = [dist.project_name for dist in pkg_resources.working_set]
print(installed_packages)

This will give you a list of all installed packages as Python objects, so you can filter or process them however you want. It’s a nice solution if you need to work with the modules programmatically!

I love those suggestions! For a bit more flexibility, you can also use sys and pkgutil. These give you access to modules in your current Python environment, including those in the standard library. Here’s an example:

import pkgutil

installed_modules = [module.name for module in pkgutil.iter_modules()]
print(installed_modules)

This approach will list all available modules, including the built-in ones, for the current environment. It’s great if you want a broader view of what’s available in Python right now!