How to List Installed Python Modules

Introduction

Python, somewhat similar ot Node, uses a system of installed modules/packages. But as you continue to install more and more modules, it might get a bit tricky to keep track of all of them. In this Byte, we'll explore how to get a list of all locally installed Python modules, which can be very helpful in managing your Python environment.

Python Modules

A Python module is basically just a file (or directory of files) containing Python definitions and statements. These modules can define functions, classes, and variables that can be utilized in other Python code. It's like a toolkit filled with various tools, each designed for a specific task. Python comes with a standard library of modules, but the beauty of Python actually lies in the vast array of 3rd party modules that are available. These can be installed locally and used as needed.

For instance, if you're working on a web scraping project, you might install the BeautifulSoup module. Or, if you're dealing with data analysis, the pandas module is popular for many data-related tasks.

How to List Installed Python Modules

Python provides a few different ways to list all the modules installed in your local environment. The most common method is using the pip command, which is the standard package manager for Python.

Using pip list

The pip list command is a quick way to see all the Python modules installed in your current environment. Open up your terminal or command line interface and type in the following command:

$ pip list

This will provide you with a list of all installed packages, along with their respective versions.

Package         Version
--------------- -------
beautifulsoup4  4.9.3
numpy           1.19.5
pandas          1.1.5
pip             21.0.1
setuptools      54.1.2

Note: Remember that the list of modules you see will be specific to your current Python environment. If you're using a virtual environment, only the modules installed in that environment will be shown.

That's it for the pip list command. It's a pretty simple way to get a quick overview of your installed Python modules. In the next section of this Byte, we'll see another useful command, pip freeze, and see how it differs from pip list.

Using pip freeze

pip freeze is another command that you can use to list all installed Python modules. But unlike pip list, pip freeze returns the list of modules in a format that pip can consume. This means each line of the output is a valid argument for pip install.

Let's run pip freeze in the command line and see what it gives us:

$ pip freeze

This will return something like:

Get free courses, guided projects, and more

No spam ever. Unsubscribe anytime. Read our Privacy Policy.

asn1crypto==0.24.0
certifi==2018.1.18
cffi==1.11.5
chardet==3.0.4
cryptography==2.1.4
idna==2.6
pycparser==2.18
PySocks==1.6.8
requests==2.18.4
six==1.11.0
urllib3==1.22

Each line in the output is a module along with its installed version. This is super helpful when you need to replicate your environment elsewhere or create your requirements.txt file.

Differences Between pip list and pip freeze

Now that you've seen both pip list and pip freeze in action, you might be wondering what the difference is, other than some simple formatting.

And the main difference between the two commands really is just the formatting of their output. pip list outputs a slightly more human-readable format of installed packages, which is great when you're quickly checking what's installed. On the other hand, pip freeze outputs a list of packages in a format that pip can use in other commands. This is particularly useful when you want to replicate your environment, as you can simply redirect the output of pip freeze to a requirements file, then use pip install -r requirements.txt on another machine to install the same packages.

Note: Both pip list and pip freeze will list all installed packages, regardless of where they were installed from. This includes packages installed via pip, setup.py, and other package managers.

Listing Modules in a Virtual Environment

Working in a virtual environment can help you manage dependencies for different Python projects and keep them separate. When you activate a virtual environment, pip list and pip freeze will only show the packages installed in that environment.

To illustrate, let's create a new virtual environment and install a package:

$ python3 -m venv myenv
$ source myenv/bin/activate
(myenv) $ pip install requests

Now, if we run pip list or pip freeze, only the requests package and its dependencies will be listed:

(myenv) $ pip list
Package    Version
---------- -------
certifi    2021.5.30
chardet    4.0.0
idna       2.10
requests   2.25.1
urllib3    1.26.6

Conclusion

To sum up, Python provides a few ways to list all installed modules, whether you're working in a global or virtual environment. pip list and pip freeze are two commands that not only list the installed packages but also provide additional info, namely the version of the packages. Remember that the list of installed packages can vary depending on your active environment, so always double-check which environment you're in before installing or listing packages.

Last Updated: September 12th, 2023
Was this helpful?
Project

Building Your First Convolutional Neural Network With Keras

# python# artificial intelligence# machine learning# tensorflow

Most resources start with pristine datasets, start at importing and finish at validation. There's much more to know. Why was a class predicted? Where was...

David Landup
David Landup
Details

Ā© 2013-2024 Stack Abuse. All rights reserved.

AboutDisclosurePrivacyTerms