
Check Installed Modules in Python
You can check the installed modules (also known as packages or libraries) in your Python environment using a few different methods. Here are three common methods:
- Using
pip list
orpip freeze
(Command Line): Open your terminal or command prompt and run one of the following commands: To list installed packages with version numbers usingpip list
:
pip list
To generate a requirements file containing all installed packages and their versions using pip freeze
:
pip freeze > requirements.txt
The requirements.txt
file will contain a list of installed packages that you can use to recreate the environment on another system.
- Using
pkg_resources
(Python Code): You can also check for installed packages programmatically using thepkg_resources
module. Here’s an example:
import pkg_resources
installed_packages = pkg_resources.working_set
installed_package_names = [package.project_name for package in installed_packages]
print(installed_package_names)
This code snippet will print the names of all installed packages in your Python environment.
- Using
pipdeptree
(Third-Party Tool):pipdeptree
is a third-party tool that can help you visualize the dependency tree of your installed packages. You can install it using pip:
pip install pipdeptree
Once installed, you can run the following command to see the dependency tree:
pipdeptree
It will display a tree-like structure of installed packages and their dependencies.
Choose the method that best suits your needs and workflow. The first two methods are built-in and can be used directly from the command line or within your Python code. The third method, using pipdeptree
, provides a more detailed view of package dependencies.