7

In which circumstances would one use one approach over the other? Are there downsides to both approaches?

I have seen both approaches, but I don't know, what the difference between both is and I can't seem to find an answer to that question. Are there differences in behaviour? Or is one just an evolved version of the other?

For example, here both approaches are used among all the answers.

Note: There also seem to be a quite similar question here, but this question only deals with the necessity of providing -m when using python -m pip install <package>, but not with the differences between the two approaches above.

Peter Lehnhardt
  • 4,375
  • 1
  • 14
  • 33
  • There's an answer to this question here: https://stackoverflow.com/questions/40392499/why-is-m-needed-for-python-m-pip-install – Thomas Kimber Jan 26 '21 at 09:28

1 Answers1

6

pip install ... invokes the pip executable which has to be on your path. python -m pip ... invokes the pip application that corresponds to this python installation (i.e. Python will use its normal import mechanism to resolve the pip package).

Usually these two approaches are the same, but think about what happens after alias pip=echo; the python -m pip ... approach is still going to work.

For example you can use the python -m pip ... approach if you're inside a virtual environment, but you want to install a package into another Python installation:

(somevenv) $ pip install ...   # installs into 'somevenv'
(somevenv) $ /path/to/othervenv/bin/python -m pip install ...  # installs into 'othervenv'

In the end, python -m pip ... is always going to use the 'correct' installation of Python, assuming that you intended to use whatever python points to.

If you want to install a distribution from within a running script then using the python -m pip approach is also the preferred way according to the pip docs.

a_guest
  • 34,165
  • 12
  • 64
  • 118