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.
4
u/[deleted] Nov 03 '19
Who says you should?
You may.
-moption means "module". When you run Python with-m Xoption, it will look for module namedX.__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 withpip.So, when you call
python -m pip, Python will executepip.__main__module, which, most likely, contains a call to the same entry point, which you would have been reached if you calledpipdirectly.However you may have different versions of
pipinstalled, they aren't necessarily tied to the Python interpreter. So, while callingpipwhenpython(orsys.executable) resolves to a Python interpreter you didn't intend, or, if, conversely, you are using the wrong version ofpip, which does resolvepythoncorrectly, you may end up with broken environment (either missing packages, or conflicting behavior of differentpipversions).Thus, running
python -m pipensures that you are using the Python that you want, and thepipversion that was installed together with this Python, and, on top of it, that whenpipwill run, it will resolvepythoncorrectly.None of this is necessary or important if you only have one instance of either Python or
pipinstalled.