“pip install"和“pip install"有什么区别?和“python -m pip install"? [英] What's the difference between "pip install" and "python -m pip install"?
问题描述
我有 Python 3.4.1 的本地版本,我可以运行 python -m pip install
,但我找不到运行 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?
推荐答案
2014
他们做的事情完全一样.事实上,分发 Python 模块的文档刚刚更新,建议使用 python -m pip
而不是 pip
可执行文件,因为这样更容易判断将使用哪个版本的 python 来实际运行 pip
.
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 :)
如果你看一下 pip
可执行脚本,它就是这样做的:
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')()
它正在调用 load_entry_point
,它返回一个函数,然后执行该函数.它使用的入口点称为 'console_scripts'
.如果您查看 pip
的 entry_points.txt 文件(我的 Ubuntu 机器上的/usr/lib/python2.7/dist-packages/pip-1.5.4.egg-info/entry_points.txt),你会看到这个:
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
所以返回的入口点是pip
模块中的main
函数.
So the entry point returned is the main
function in the pip
module.
当您运行 python -m pip
时,您正在执行 pip
包内的 __main__.py
脚本.看起来像这样:
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)
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()
如您所见,它也只是调用了 pip.main
函数.因此,这两个命令最终都会调用 pip/__init__.py
中的相同 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
.
这篇关于“pip install"和“pip install"有什么区别?和“python -m pip install"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!