37

I am trying to install some packages using pip and python3. I am using MacOS, so by default when I run pip, it uses my version of Python 2.

I have been able to install a package in python 3 by using:

$ pip3 install package_name

However, I am able to do the same by (at least it seems):

$ python3 -m pip install package_name

I wonder whether or not pip3 and python3 -m pip have the same effect.

lmiguelvargasf
  • 63,191
  • 45
  • 217
  • 228
  • 2
    Not specific to Python, but... Your shell could behave differently, because it's looking up two different names. Sometimes, new software is installed out under `/usr/local/` or weirder places, and the only concession to the typical `$PATH` or filesystem hierarchy is a hand-crafted symlink like `/usr/bin/python3 -> /opt/experimental/python3.2-local-myedit/bin/python`. Often in these setups, only the "star" binary like `python` or `perl` gets symlinked, while everything else from related binaries (like `pip`) to manual pages send users to whatever came on the OS's installation media. – Kevin J. Chase Dec 23 '16 at 21:29
  • 1
    Personal scripts, shell functions, and aliases are subject to similar errors. Example: Sourcing some config file before running `python`, but not `pip`. – Kevin J. Chase Dec 23 '16 at 21:32

3 Answers3

27

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

tihom
  • 7,923
  • 1
  • 25
  • 29
  • 1
    thank you, I know how to see the code of `pip3` (`$ less /usr/local/bin/pip3`), but how did you know the code that `python3 -m pip install ....` uses? – lmiguelvargasf Dec 23 '16 at 21:36
  • as Kevin J. Chase suggests, it is possible that the shell is looking up two different names. Is there any way to ensure what you are stating? I mean how to know the code that is executed with `$ python3 -m pip install ...` – lmiguelvargasf Dec 23 '16 at 21:39
  • 1
    `python3 -m module` would run the the `__init__.py` file of the module. See this [SO post](http://stackoverflow.com/questions/22241420/execution-of-python-code-with-m-option-or-not) for more detailed explanation – tihom Dec 23 '16 at 21:39
  • 2
    barring some weird setup. The path for both `pip3` and `python3` should be the same `bin` folder. In that case they would call the same `pip` module. – tihom Dec 23 '16 at 21:41
  • 1
    @tihorn thanks for the tip, I just got bitten by one of those "weird setups". The path for pip3 was `/usr/bin/pip3` and the path for python3 was `~/anaconda3/bin/python3`. I was installing packages with pip3 and then python3 was telling me `ModuleNotFound`. – craq Jul 22 '19 at 23:55
9

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.

James Mishra
  • 4,249
  • 4
  • 30
  • 35
8

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.

James Mishra
  • 4,249
  • 4
  • 30
  • 35
craq
  • 1,441
  • 2
  • 20
  • 39