“pip install”和“python -m pip install”有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25749621/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's the difference between "pip install" and "python -m pip install"?
提问by ilciavo
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?
我有本地版本的 Python 3.4.1 并且可以运行python -m pip install,但是我找不到要运行的 pip 二进制文件pip install。这两者有什么区别?
采纳答案by dano
2014
2014年
They do exactly the same thing. In fact, the docs for distributing Python modules were just updatedto suggest using python -m pipinstead of the pipexecutable, because it's easier to tell which version of python is going to be used to actually run pipthat way.
他们做的事情完全一样。事实上,用于分发 Python 模块的文档刚刚更新以建议使用python -m pip而不是pip可执行文件,因为更容易判断将使用哪个版本的 Python 来实际运行pip。
Here's some more concrete "proof", beyond just trusting my word and the bug report I linked :)
这是一些更具体的“证据”,不仅仅是相信我的话和我链接的错误报告:)
If you take a look at the pipexecutable script, it's just doing this:
如果你看一下pip可执行脚本,它只是这样做:
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:
它正在调用load_entry_point,它返回一个函数,然后执行该函数。它使用的入口点称为'console_scripts'。如果你pip在我的 Ubuntu 机器上查看 entry_points.txt 文件(/usr/lib/python2.7/dist-packages/pip-1.5.4.egg-info/entry_points.txt),你会看到:
[console_scripts]
pip = pip:main
pip2.7 = pip:main
pip2 = pip:main
So the entry point returned is the mainfunction in the pipmodule.
所以返回的入口点是模块中的main函数pip。
When you run python -m pip, you're executing the __main__.pyscript inside the pippackage. That looks like this:
当您运行时python -m pip,您正在执行包__main__.py内的脚本pip。看起来像这样:
import sys
from .runner import run
if __name__ == '__main__':
exit = run()
if exit:
sys.exit(exit)
And the runner.runfunction looks like this:
该runner.run函数如下所示:
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.mainfunction, too. So both commands end up calling the same mainfunction in pip/__init__.py.
如您所见,它也只是调用了该pip.main函数。所以,这两个命令最终调用相同main的功能pip/__init__.py。

