6

How do I list packages that I have directly installed with pip, omitting any dependences that have been installed as a result?

I have a virtualenv in which I've run, commands like

$ pip install A B C
$ pip install X Y Z

as a result of which I have installed not only A, B, C, X, Y, and Z, but also dependences p, q, r, s, t, u, v, and w. But looking at any given package, I can't recall whether it was something I installed directly or not. I would like to keep the directly installed packages in this venv uptodate with something like

$ pip install -U --directly-installed

that has the effect of

$ pip install -U A B C X Y Z

Is there a way to keep only the directly installed packages explicitly up to date (updating their dependences only as required by those packages)?

orome
  • 45,163
  • 57
  • 202
  • 418
  • You can't, as `pip` doesn't track whether a package was installed explicitly or as a dependency. With tools like `pipdeptree` or `pip-chill`, you can get a list of "top-level" packages in the sense that they are not a dependency of some other package. These are definitely those you have installed explicitly (with an exception of `pip`, `setuptools` and `wheel` if you're in a venv). – hoefling Dec 26 '18 at 16:34
  • However, this list would not be complete/reliable enough - e.g. imagine you `pip install numpy`, then `pip install scipy`. Now, although `numpy` was installed explicitly, it becomes a dependency of `scipy`, so it won't be listed as a "top-level" package anymore. – hoefling Dec 26 '18 at 16:35
  • @hoefling That sounds like the answer then: I need to go back and freeze, figure out what I install directly, prune `requirements.txt` and then just `pip -U requirements.txt`. Right? – orome Dec 26 '18 at 16:54
  • Yes. After you have a clean requirements file, handle it as kind of a "world" file where you put all the packages to be installed, as @phd described in his answer: the installation of `pkg` becomes now smth like `echo pkg >> world.txt && pip install -r world.txt`. – hoefling Dec 26 '18 at 18:04

2 Answers2

6

At the job we handle the lists of directly installed packages manually. That is, if we need a package we add it to requirements.txt and run

pip install -r requirements.txt

After that we run

pip freeze > requirements-freezed.txt

to save the entire list of packages and dependencies. When we need to recreate a virtual environment we run

pip install -r requirements-freezed.txt

Your task to upgrade only directly installed packages would be

pip install -U -r requirements.txt
pip freeze > requirements-freezed.txt
phd
  • 82,685
  • 13
  • 120
  • 165
  • Will using the `pip` that's in a virtualenv install packages in that virtualenv, or does the virtualenv also need be active when that `pip` is run? – orome Dec 26 '18 at 19:18
  • 1
    virtualenv needs to be activated but you can do that semimagically just by running `python` or `pip` from the `bin/` dir. Something like `/path/to/virt/env/bin/python -m pip install…` – phd Dec 26 '18 at 19:48
  • There's a question about how to apply that in a PyCharm External Tool [here](https://stackoverflow.com/q/53936169/656912). – orome Dec 26 '18 at 20:06
-1

From the pip documentation:

--upgrade-strategy Determines how dependency upgrading should be handled [default: only-if-needed]. “eager” - dependencies are upgraded regardless of whether the currently installed version satisfies the requirements of the upgraded package(s). “only-if-needed” - are upgraded only when they do not satisfy the requirements of the upgraded package(s).

So it seems what you want is already the default behavior, but if you want to be explicit:

pip install -U --upgrade-strategy only-if-needed A B C X Y Z
lmarchesoti
  • 16
  • 1
  • 4
  • Sorry: I don't recall what A B C X Y Z are! If I remembered which packages were there because I asked for them, and which were installed as dependencies, there would indeed be no issue. – orome Dec 26 '18 at 16:17
  • Maybe this answer will be useful to you then: https://stackoverflow.com/a/40074640/5768509 It links to a utility that extracts the minimum environment for your project. I have not used it, so I can't guarantee... Besides that, you can try upgrading each package of interest individually. If you don't know what you want to update, then why update anything at all? – lmarchesoti Dec 26 '18 at 16:27