Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between "pip install" and "python -m pip install"?

I have a local version of Python 3.4.1 and I can run python -m pip install, but I'm unable to find the pip binary to run pip install. What's the difference between these two?

like image 387
ilciavo Avatar asked Sep 09 '14 16:09

ilciavo


People also ask

What is difference between pip and Python pip?

PIP is a soft link for a particular installer. pip3 is an updated version of pip which is used basically for python 3+. The system will use one of your Python versions depending on what exactly is first in the system PATH variable. When you run PIP3, you can be sure that the module will be installed in Python 3.

What is Python M pip install?

PIP is a package management system used to install and manage software packages written in Python. It stands for “preferred installer program” or “Pip Installs Packages.” PIP for Python is a utility to manage PyPI package installations from the command line.

Does installing pip install Python?

Key terms. pip is the preferred installer program. Starting with Python 3.4, it is included by default with the Python binary installers. A virtual environment is a semi-isolated Python environment that allows packages to be installed for use by a particular application, rather than being installed system wide.

What is '- M in Python?

The -m stands for module-name in Python. The module-name should be a valid module name in Python. The -m flag in Python searches the sys. path for the named module and executes its contents as the __main__ module.


1 Answers

2014

They do exactly the same thing. In fact, the docs for distributing Python modules were just updated to suggest using python -m pip instead of the pip executable, because it's easier to tell which version of python is going to be used to actually run pip that way.


Here's some more concrete "proof", beyond just trusting my word and the bug report I linked :)

If you take a look at the pip executable script, it's just doing this:

from pkg_resources import load_entry_point <snip> load_entry_point('pip==1.5.4', 'console_scripts', 'pip')() 

It's calling load_entry_point, which returns a function, and then executing that function. The entry point it's using is called 'console_scripts'. If you look at the entry_points.txt file for pip (/usr/lib/python2.7/dist-packages/pip-1.5.4.egg-info/entry_points.txt on my Ubuntu machine), you'll see this:

[console_scripts] pip = pip:main pip2.7 = pip:main pip2 = pip:main 

So the entry point returned is the main function in the pip module.

When you run python -m pip, you're executing the __main__.py script inside the pip package. That looks like this:

import sys from .runner import run  if __name__ == '__main__':     exit = run()     if exit:         sys.exit(exit) 

And the runner.run function looks like this:

def run():     base = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))     ## FIXME: this is kind of crude; if we could create a fake pip     ## module, then exec into it and update pip.__path__ properly, we     ## wouldn't have to update sys.path:     sys.path.insert(0, base)     import pip     return pip.main() 

As you can see, it's just calling the pip.main function, too. So both commands end up calling the same main function in pip/__init__.py.

like image 128
dano Avatar answered Sep 23 '22 05:09

dano