200

Why does pip list generate a more comprehensive list than pip freeze?

$ pip list
feedparser (5.1.3)
pip (1.4.1)
setuptools (1.1.5)
wsgiref (0.1.2)
$ pip freeze
feedparser==5.1.3
wsgiref==0.1.2

Pip's documentation states:

   
freeze Output installed packages in requirements format.
list List installed packages.

What is a "requirements format"?

Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
nitrl
  • 2,185
  • 2
  • 15
  • 15

8 Answers8

178

One may generate a requirements.txt via:

$ pip freeze > requirements.txt

A user can use this requirements.txt file to install all the dependencies. For instance:

$ pip install -r requirements.txt

The packages need to be in a specific format for pip to understand, such as:

# requirements.txt
feedparser==5.1.3
wsgiref==0.1.2
django==1.4.2
...

That is the "requirements format".

Here, django==1.4.2 implies install django version 1.4.2 (even though the latest is 1.6.x). If you do not specify ==1.4.2, the latest version available would be installed.

You can read more in "Virtualenv and pip Basics", and the official "Requirements File Format" documentation.

Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
karthikr
  • 97,368
  • 26
  • 197
  • 188
  • 10
    Got it. Is there any particular reason why "list" produces a more comprehensive list than "freeze"? – nitrl Sep 24 '13 at 14:52
  • 2
    i think it is because `pip list` lists everything, and `pip freeze` installs everything installed by pip. – karthikr Sep 24 '13 at 14:55
  • Hm, that's a theory, but I'm fairly sure I didn't `pip install` `wsgiref`. – nitrl Sep 24 '13 at 15:17
  • 1
    Python 3.2 includes `wsgiref.egg-info` in the Lib directory, which is why pip knows about it. You can't [un]install it with pip, and later versions of Python omit the metadata file so it won't appear. – Zooba Mar 17 '14 at 18:39
  • you can use requirements.txt regardless of whether you're using a virtualenv or not – Fabio Sep 18 '16 at 04:50
  • @nitrl some of the packages you see there were installed as dependencies to others – Fabio Sep 18 '16 at 04:51
  • Is there a way to install the latest versions of the packages and not the specific ones, and is it a good idea to do so? – Leonid Sep 29 '17 at 01:33
  • 1
    @leonid if you get rid of the version number at the end , it would install the latest version from pypi. Read the answer completely – karthikr Sep 29 '17 at 04:04
  • 11
    This answer doesn't address the most puzzling part of the question - why do the two lists differ? It only addresses the self-evident part of the question - requirements format is the format output by 'pip freeze', an example of which is shown in the question. – Jonathan Hartley Jun 06 '19 at 16:05
71

To answer the second part of this question, the two packages shown in pip list but not pip freeze are setuptools (which is easy_install) and pip itself.

It looks like pip freeze just doesn't list packages that pip itself depends on. You may use the --all flag to show also those packages.

From the documentation:

--all

Do not skip these packages in the output: pip, setuptools, distribute, wheel

ForeverWintr
  • 5,492
  • 2
  • 36
  • 65
61

The main difference is that the output of pip freeze can be dumped into a requirements.txt file and used later to re-construct the "frozen" environment.

In other words you can run: pip freeze > frozen-requirements.txt on one machine and then later on a different machine or on a clean environment you can do: pip install -r frozen-requirements.txt and you'll get the an identical environment with the exact same dependencies installed as you had in the original environment where you generated the frozen-requirements.txt.

Daniel Lahyani
  • 925
  • 8
  • 15
31

Look at the pip documentation, which describes the functionality of both as:

pip list

List installed packages, including editables.

pip freeze

Output installed packages in requirements format.

So there are two differences:

  1. Output format, freeze gives us the standard requirement format that may be used later with pip install -r to install requirements from.

  2. Output content, pip list include editables which pip freeze does not.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Serjik
  • 10,543
  • 8
  • 61
  • 70
20

pip list shows ALL installed packages.

pip freeze shows packages YOU installed via pip (or pipenv if using that tool) command in a requirements format.

Remark below that setuptools, pip, wheel are installed when pipenv shell creates my virtual envelope. These packages were NOT installed by me using pip:

test1 % pipenv shell
Creating a virtualenv for this project…
Pipfile: /Users/terrence/Development/Python/Projects/test1/Pipfile
Using /usr/local/Cellar/pipenv/2018.11.26_3/libexec/bin/python3.8 (3.8.1) to create virtualenv…
⠹ Creating virtual environment...
<SNIP>
Installing setuptools, pip, wheel...
done.
✔ Successfully created virtual environment! 
<SNIP>

Now review & compare the output of the respective commands where I've only installed cool-lib and sampleproject (of which peppercorn is a dependency):

test1 % pip freeze       <== Packages I'VE installed w/ pip

-e git+https://github.com/gdamjan/hello-world-python-package.git@10<snip>71#egg=cool_lib
peppercorn==0.6
sampleproject==1.3.1


test1 % pip list         <== All packages, incl. ones I've NOT installed w/ pip

Package       Version Location                                                                    
------------- ------- --------------------------------------------------------------------------
cool-lib      0.1  /Users/terrence/.local/share/virtualenvs/test1-y2Zgz1D2/src/cool-lib           <== Installed w/ `pip` command
peppercorn    0.6       <== Dependency of "sampleproject"
pip           20.0.2  
sampleproject 1.3.1     <== Installed w/ `pip` command
setuptools    45.1.0  
wheel         0.34.2
F1Linux
  • 3,580
  • 3
  • 25
  • 24
  • pip list - shows installed packages, not ALL. Check official documentation and the answer from Daniel Lahyani. – filler36 Aug 17 '20 at 12:57
  • 2
    @filler36: I thought that "installed" was implied, but nonetheless incorporated your feedback to ensure the ambiguity didn't create a potential confusion. Thanks for your feedback, very much obliged!- T – F1Linux Aug 17 '20 at 13:26
  • 1
    "pip freeze shows packages YOU installed via pip". On Linux this is WRONG! Both list and freeze show ALL packages, including those installed by the system's package manager (which is NOT YOU). `pip freeze` omits only (setuptools, wheel, pip, distribute), but has an `all` option to include them. [documentation](https://pip.pypa.io/en/stable/cli/pip_freeze/#cmdoption-all). – j77h Jun 12 '23 at 09:52
16

My preferred method of generating a requirements file is:

pip list --format=freeze > requirements.txt

This method keeps just the package names and package versions without potentially linking to local file paths which 'pip freeze' alone will sometimes give me. Local file paths in a requirements file make your codebase harder to use for other users and some developers don't know how to fix this so I prefer this method for ease of adoptability.

colby-ham
  • 457
  • 9
  • 13
  • 1
    Wouldn't `pip list --not-required --format=freeze > requirements.txt` be even better since it only includes the top-level dependencies? – dijonkitchen May 03 '23 at 16:10
  • @dijonkitchen thanks for your input! I haven't done a deep dive into using this flag but I think it probably makes a requirements file cleaner and easier to understand. I am seeing some issues reported for it missing some particular dependencies which could be a potential issue but probably few and far between. Overall I'm in favor of explicit environment configurations and minimizing nondeterministic decisions so I would say in a data quality critical application like scientific computing it's useful to list all dependencies, but for most applications, top level is good enough. – colby-ham May 03 '23 at 22:03
  • 1
    Yeah, it may exclude some top-level dependencies if they are also nested in another one. The only problem I see with explicit dependencies all the way down is it's difficult to upgrade and you may have extra ones that are no longer needed when nested dependencies change. – dijonkitchen May 04 '23 at 16:34
1
pip list

List installed packages: show ALL installed packages that even pip installed implictly

pip freeze

List installed packages: - list of packages that are installed using pip command

pip freeze has --all flag to show all the packages.

Other difference is the output it renders, that you can check by running the commands.

Sunil Garg
  • 14,608
  • 25
  • 132
  • 189
  • On Linux, both list and freeze show ALL packages, including those installed by the system's package manager (i.e. not installed by pip or by you). pip freeze omits ONLY (setuptools, wheel, pip, distribute). – j77h Jun 12 '23 at 10:10
0

For those looking for a solution. If you accidentally made pip requirements with pip list instead of pip freeze, and want to convert into pip freeze format. I wrote this R script to do so.

library(tidyverse)

pip_list = read_lines("requirements.txt")

pip_freeze = pip_list %>%
  str_replace_all(" \\(", "==") %>%
  str_replace_all("\\)$", "")

pip_freeze %>% write_lines("requirements.txt")
CoderGuy123
  • 6,219
  • 5
  • 59
  • 89