Intrastellar Explorer
Intrastellar Explorer

Reputation: 2411

Does `pip install -U pip -r requirements.txt` upgrade pip before installing the requirements?

It seems to be common practice to set up a Python virtual environment using some variant of the following:

python -m venv venv && source ./venv/bin/activate
python -m pip install -U pip -r requirements.txt

What I hope the above command does is:

  1. Upgrade pip first
  2. Run the installation of the packages in requirements.txt

However, what actually seems to happen is:

  1. Collects all packages, including newest version of pip
  2. Installs them all together
    • The original/outdated version of pip is what actually runs the installs
    • And the new version of pip is not used until after this command

Question(s)

  1. Is it possible to have pip upgrade itself and then install a requirements file, in one command?
    • Would this infer any particular benefits?
  2. Should I switch to the following?
python -m venv venv && source ./venv/bin/activate
python -m pip install -U pip
python -m pip install -r requirements.txt
  1. What is the optimal method to install requirements files?
    • I see people sometimes installing/upgrading wheel and setuptools as well

Upvotes: 1

Views: 4850

Answers (3)

devdanke
devdanke

Reputation: 1590

Here's what pip install -h says about -U:

  -U, --upgrade   Upgrade all specified packages to the newest available
                  version. The handling of dependencies depends on the
                  upgrade-strategy used.

Which I think means: Install the latest version of a library. If it's already installed but isn't the latest version, then update it to the latest version.

Upvotes: 0

Dilman Salih
Dilman Salih

Reputation: 111

I had a situation similar to yours, I needed to first upgrade pip and then install a bunch of libraries in a lab that had 20 PCs. What I did was writing all the librarie's name in a requirements.txt file, then create a .bat file with two commands:

`python -m pip install --upgrade pip`
`pip install -r requirements.txt`

The first command for upgrading pip and the second one for installing all the libraries listed in the requirements.txt file.

Upvotes: 1

holdenweb
holdenweb

Reputation: 37033

The answers to your questions are:

  1. No. pip doesn't currently treat itself as a special dependency, so it doesn't know to install then execute itself, which is what it would need to do to overcome the problems you observed.
  2. Updating pip in a separate step is indeed the recommended way to proceed.

You may from time to time see pip issue a message advising that a newer version is available. This happens a lot if you create them from a python with an outdated pip.

Upvotes: 5

Related Questions