简体   繁体   English

在终端中使用 Python 3 和 pip3 安装软件包有什么区别?

[英]What is the difference between installing packages with Python 3 and pip3 in terminal?

I have two versions of Python on my Mac: 2.7 and 3.8.我的 Mac 上有两个版本的 Python:2.7 和 3.8。

When installing packages, I use pip3 instead of pip in order for the packages so install for the correct Python version.安装软件包时,我使用pip3而不是pip以便安装正确的 Python 版本。

However, I noticed that I can also install with Python3 in the terminal:但是,我注意到我也可以在终端中安装Python3

 python3 -m pip install ...

Is there a difference between the two?两者有区别吗? I noticed they showed different pip versions which was a bit strange.我注意到他们展示了不同的 pip 版本,这有点奇怪。

https://docs.python.org/3/using/cmdline.html#cmdoption-m https://docs.python.org/3/using/cmdline.html#cmdoption-m

Using -m is another way of executing an installed module.使用 -m 是另一种执行已安装模块的方式。 More specifically, it would be a way of using modules installed for different python versions.更具体地说,这将是一种使用为不同 python 版本安装的模块的方法。

python2 -m pip install ...
python3 -m pip install ...

Run flask module installed on python2 or python3
python2 -m flask
python3 -m flask

For your case you probably want to install a module on python 3.8 as 2.7 is the default installation that comes with the OS and not somewhere you'd want to install modules for development.对于您的情况,您可能希望在 python 3.8 上安装一个模块,因为 2.7 是操作系统附带的默认安装,而不是您想要安装用于开发的模块的地方。

python -m pip install... is the same as using pip, but pip3 is for python3. python -m pip install...与使用 pip 相同,但 pip3 用于 python3。 The only thing you need to change is "python" to "python3" like this: python3 -m pip install...您唯一需要更改的是将“python”更改为“python3”,如下所示: python3 -m pip install...

Edit: python3 is used when running scripts with python 3.8, while "python" is with python 2.7.编辑:使用 python 3.8 运行脚本时使用 python3,而“python”使用 python 2.7。 pip3 is mainly if you want the libraries installed for python3, and you don't really care about 2.7. pip3 主要是如果您想要为 python3 安装库,并且您并不真正关心 2.7。

This is what you need to know:这是你需要知道的:

In short:简而言之:

Always use the path/to/pythonX.Y -m pip somecommand form, known as the executable module (or executable package ).始终使用path/to/pythonX.Y -m pip somecommand形式,称为可执行模块(或可执行 package )。 Never use the script form pip , pip3 , or anything like that, because there is no guarantee which Python interpreter is associated with a script (unless you actually have everything under control and check yourself).切勿使用pippip3或类似形式的脚本,因为无法保证哪个 Python 解释器与脚本相关联(除非您实际上可以控制一切并检查自己)。

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

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