简体   繁体   English

pip3 和 python3 -m pip 的区别

[英]Difference between pip3 and python3 -m pip

I am trying to install some packages using pip and python3.我正在尝试使用 pip 和 python3 安装一些软件包。 I am using MacOS, so by default when I run pip, it uses my version of Python 2.我使用的是 MacOS,所以默认情况下,当我运行 pip 时,它使用我的 Python 2 版本。

I have been able to install a package in python 3 by using:我已经能够使用以下方法在 python 3 中安装一个包:

$ pip3 install package_name

However, I am able to do the same by (at least it seems):但是,我可以通过(至少看起来)来做同样的事情:

$ python3 -m pip install package_name

I wonder whether or not pip3 and python3 -m pip have the same effect.我想知道pip3python3 -m pip是否具有相同的效果。

They are the same.他们是一样的。 If you look at the pip3 file in the bin folder it calls the main function from the pip module.如果您查看 bin 文件夹中的 pip3 文件,它会从 pip 模块调用main函数。

pip3 install package_name runs pip3 file in the bin folder: pip3 install package_name运行 bin 文件夹中的 pip3 文件:

# bin/pip3 
# or bin/pip if using pip install package_name

import re
import sys

from pip import main

if __name__ == '__main__':
    sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
    sys.exit(main())

python3 -m pip install package_name runs the __init__.py file of the pip module. python3 -m pip install package_name运行pip模块的__init__.py文件。

# pip/__init__.py
if __name__ == '__main__':
    sys.exit(main())

Both of them run the same main() function它们都运行相同的main()函数

As @tihorn says, pip3 and python3 -m pip should be the same.正如@tihorn 所说, pip3python3 -m pip应该是一样的。 There is at least one exception: if they are not in the same path.至少有一个例外:如果它们不在同一路径中。 I had the following setup:我有以下设置:

$ which pip3
/usr/bin/pip3
$ which python3
/home/username/anaconda3/bin/python3

After installing modules with pip3 and verifying with pip3 freeze , I was not able to access them when running python3 my_script.py or python3 -c 'import my_module' .使用 pip3 安装模块并使用pip3 freeze验证后,我在运行python3 my_script.pypython3 -c 'import my_module'时无法访问它们。 I was getting a ModuleNotFound error.我收到ModuleNotFound错误。

The other answers are technically correct, but are a little unclear on why Python has both pip3 and python3 -m pip :其他答案在技术上是正确的,但有点不清楚为什么Python 同时具有pip3python3 -m pip

Using pip3 to globally install a package can be ambiguous if you have multiple Python installations on your machine.如果您的机器上有多个 Python 安装,使用pip3全局安装包可能会产生歧义

Many people end up with multiple Python installations after they upgrade their computer's operating system.许多人在升级计算机的操作系统后最终安装了多个 Python。 OS upgrades usually install a new Python, but they do not risk purging the old Python and breaking existing programs on the computer.操作系统升级通常会安装一个新的 Python,但它们不会冒清除旧 Python 和破坏计算机上现有程序的风险。

For these reasons, on my own computer, I always install with the specific version, eg: python3.8 -m pip .由于这些原因,在我自己的计算机上,我总是安装特定版本,例如: python3.8 -m pip When I am writing Makefiles or build scripts to distribute to others, I default to python3 -m pip but let the user optionally replace python3 with their own interpreter path.当我编写 Makefile 或构建脚本以分发给其他人时,我默认使用python3 -m pip但让用户可以选择用他们自己的解释器路径替换python3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM