r/programming • • Nov 03 '19

Why you should use `python -m pip`

https://snarky.ca/why-you-should-use-python-m-pip/
0 Upvotes

13 comments sorted by

View all comments

4

u/[deleted] Nov 03 '19

Who says you should?

You may. -m option means "module". When you run Python with -m X option, it will look for module named X.__main__ and will execute its code. Traditionally, __main__ module contains code related to command line arguments parsing and the program's entry point. This is also the case with pip.

So, when you call python -m pip, Python will execute pip.__main__ module, which, most likely, contains a call to the same entry point, which you would have been reached if you called pip directly.

However you may have different versions of pip installed, they aren't necessarily tied to the Python interpreter. So, while calling pip when python (or sys.executable) resolves to a Python interpreter you didn't intend, or, if, conversely, you are using the wrong version of pip, which does resolve python correctly, you may end up with broken environment (either missing packages, or conflicting behavior of different pip versions).

Thus, running python -m pip ensures that you are using the Python that you want, and the pip version that was installed together with this Python, and, on top of it, that when pip will run, it will resolve python correctly.


None of this is necessary or important if you only have one instance of either Python or pip installed.