orome
orome

Reputation: 48466

Can I keep only directly installed packages up to date with pip?

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)?

Upvotes: 6

Views: 916

Answers (2)

phd
phd

Reputation: 94473

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

Upvotes: 6

lmarchesoti
lmarchesoti

Reputation: 16

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

Upvotes: -1

Related Questions