Difference between pip3 and python3 -m pip
As @tihorn says, pip3
and python3 -m pip
should be the same. There is at least one exception: if they are not in the same path. I had the following setup:
$ which pip3
/usr/bin/pip3
$ which python3
/home/username/anaconda3/bin/python3
After installing modules with pip3 and verifying with pip3 freeze
, I was not able to access them when running python3 my_script.py
or python3 -c 'import my_module'
. I was getting a ModuleNotFound
error.
They are the same. If you look at the pip3 file in the bin folder it calls the main
function from the pip module.
pip3 install package_name
runs pip3 file in the bin folder:
# bin/pip3
# or bin/pip if using pip install package_name
import re
import sys
from pip import main
if __name__ == '__main__':
sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
sys.exit(main())
python3 -m pip install package_name
runs the __init__.py
file of the pip module.
# pip/__init__.py
if __name__ == '__main__':
sys.exit(main())
Both of them run the same main()
function
The other answers are technically correct, but are a little unclear on why Python has both pip3
and python3 -m pip
:
Using pip3
to globally install a package can be ambiguous if you have multiple Python installations on your machine.
Many people end up with multiple Python installations after they upgrade their computer's operating system. OS upgrades usually install a new Python, but they do not risk purging the old Python and breaking existing programs on the computer.
For these reasons, on my own computer, I always install with the specific version, e.g: python3.8 -m pip
. When I am writing Makefiles or build scripts to distribute to others, I default to python3 -m pip
but let the user optionally replace python3
with their own interpreter path.