11

I recently used pip to install the requests package in python 2.7, however in order to do so I had to use:

python -m pip install requests 

instead of just:

python pip install requests

which gave me an error:

can't open file 'pip: [Errno 2] No such file or directory

Why did I need to add the -m?

Rob Bednark
  • 25,981
  • 23
  • 80
  • 125
Mwspencer
  • 1,142
  • 3
  • 18
  • 35
  • Are those the complete command you used? (I suspect not) Did you have `python ` in front of each of these? – cco Nov 03 '16 at 04:28
  • I did yes, the entire command was > python -m pip install requests – Mwspencer Nov 03 '16 at 16:07
  • 1
    Possible duplicate of [What's the difference between pip install and python -m pip install?](http://stackoverflow.com/questions/25749621/whats-the-difference-between-pip-install-and-python-m-pip-install) – Mihai Capotă Apr 24 '17 at 16:56

1 Answers1

12

python -m pip tells python to run with the pip module as the main module.

python pip isn't understood, because pip isn't a command line argument that python understands (i.e., pip is a module).

If the python scripts directory (c:\python27\scripts for python 2.7 on windows) is on your path, then you can just run pip (without python before it) and pass the same options you would pass to python -m pip.

So: you need to add -m pip so python knows what module to use as the main module. pip is a standalone program installed in your python scripts directory, not an argument to python.

cco
  • 5,873
  • 1
  • 16
  • 21
  • 1
    you can control the target directory of installation if you used python -m pip – KawaiKx Jun 25 '21 at 06:36
  • how is this different from just doing `pip install `? – Redoman Feb 22 '22 at 06:19
  • 1
    `python -m pip install ...` and `pip install ...` are the same if the python scripts directory is on your path. On windows, you need to use `python -m pip install -U pip` to upgrade pip itself, because otherwise pip.exe (in the scripts directory) can't be replaced and the install will fail. – cco Mar 01 '22 at 22:53