pip freeze vs. pip list: Understanding the Differences
Both pip freeze and pip list are essential commands for managing Python packages, but they serve distinct purposes.
This guide clarifies the differences between pip freeze and pip list, explaining when and why you'd use each one, and how they relate to virtual environments and requirements.txt files.
pip freeze
pip freeze is primarily used to output a list of installed packages in a format that can be used to recreate the environment. This is crucial for reproducibility and sharing your project's dependencies.
Generating requirements.txt
The most common use of pip freeze is to generate a requirements.txt file:
pip freeze > requirements.txt
# OR, for Python 3
pip3 freeze > requirements.txt
This command redirects the output of pip freeze into a file named requirements.txt. This file can then be used to install the exact same package versions on another system or in a new environment:
pip install -r requirements.txt
# OR
pip3 install -r requirements.txt
Output Format
The output of pip freeze is in the format package_name==version, which is the format pip expects for specifying package versions during installation. Example:
requests==2.28.1
urllib3==1.26.12
certifi==2022.9.24
# ... other packages ...
Dependencies
By default, pip freeze lists only the top-level packages you've explicitly installed. If you installed requests, it will not list urllib3 or certifi separately (even though requests depends on them), unless you've also installed them directly with pip install.
Using --all option
You can include packages such as pip, wheel, and setuptools in the output by calling pip freeze with --all option:
pip freeze --all
pip list
pip list shows all installed packages in the current environment, including those installed as dependencies, in a more human-readable, tabular format. It's for inspecting the environment, not for recreating it.
pip list
# OR
pip3 list
Output Format
Package Version
--------------- -------
certifi 2022.9.24
pip 22.0.4
requests 2.28.1
setuptools 58.1.0
urllib3 1.26.12
wheel 0.37.1
# ...and so on
pip list displays a table with Package names and their versions.
Key Differences Summarized
| Feature | pip freeze | pip list |
|---|---|---|
| Purpose | Generate a list of installed packages suitable for recreating the environment (usually in a requirements.txt file). | List all installed packages in a human-readable format. |
| Output | package==version format | Tabular format: Package Version |
| Dependencies | Shows only top-level packages (those directly installed), not all dependencies unless explicitly installed. | Shows all installed packages, including dependencies and editable installs. |
| Use Case | Creating requirements.txt files; sharing project dependencies; creating reproducible environments. | Inspecting the current environment; seeing what's installed, including dependencies. |
Virtual Environments: Best Practice
It's strongly recommended to use virtual environments with both pip freeze and pip list. This isolates your project's dependencies and avoids conflicts.
# Create:
python3 -m venv venv # Recommended
# OR
python -m venv venv
# OR (Windows)
py -m venv venv
# Activate (Linux/macOS - bash/zsh):
source venv/bin/activate
# Activate (Windows - Command Prompt):
venv\Scripts\activate.bat
# Activate (Windows - PowerShell):
venv\Scripts\Activate.ps1
# Now pip freeze or pip list will show ONLY the packages in this environment.
pip freeze > requirements.txt
pip list
- Create a virtual environment and activate it using the correct command for your operating system.
- The
pip freezeandpip installcommands are then executed in the virtual environment.
Conclusion
pip freeze and pip list are both valuable tools for managing Python packages.
pip freezeis essential for creating reproducible environments by generatingrequirements.txtfilespip listgives you a complete overview of your current environment.
Always use virtual environments to isolate your project dependencies and avoid conflicts.