4

What is the difference between:

pip install --upgrade pip

and

python -m pip install --upgrade pip

and why is python -m pip install --upgrade pip generally favoured?

moth
  • 345
  • 1
  • 13

3 Answers3

6

The difference is between pip and python -m pip; the rest of the command doesn't matter. The reason to prefer the latter is that you're ensuring that the python you normally use is the one which will provide the pip module you invoke. Otherwise, there is a risk that the pip executable found in your PATH is from an unrelated or out of date Python installation; it might install packages, but your regular python invocation won't find them (because they're installed for a non-default Python).

On Windows, the distinction is even more important, as pip.exe can't be replaced while it's executing, so executing pip via the module with -m makes it possible to upgrade it in-place (typically, on Windows, you'd use the py.exe launcher, as py -3 -mpip, changing -3 as desired to invoke a specific version of Python for which you wish to perform the upgrade)

You can also modify the second command to invoke specific Python executable names (python2.7 vs. python3.8), or even absolute paths if you might have versions with the same name installed in multiple places.

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
0

The first one

pip install --upgrade pip

is to invoke pip as a command. The actual python interpretor called is not explicit. The second one is calling the python interpretor explicitly, so you know which one is called.

There should be no difference, as the __main__.py in the module and the pip script are both point to the same entry point, unless in the case that the default python is different from the one used by the pip script

adrtam
  • 6,991
  • 2
  • 12
  • 27
-1

If i am correct, pip install --upgrade pip and python -m pip install --upgrade pip are the same unless you specify the pip or python version. The latter is preferred because it attempts to upgrade the pip associated with the specified python version (e.g. python3.7 -m pip install --upgrade pip) even if the main python version is different (python command may refer to any python version).

Shimon Cohen
  • 489
  • 3
  • 11