"pip install"和"pip install"之间有什么区别和"python -m pip install"? [英] What's the difference between "pip install" and "python -m pip install"?

查看:689
本文介绍了"pip install"和"pip install"之间有什么区别和"python -m pip install"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有Python 3.4.1的本地版本,可以运行python -m pip install,但是找不到运行pip install的pip二进制文件.两者有什么区别?

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而不是可执行文件,因为这样更容易确定将使用哪个版本的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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆