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

久未见 提交于 2021-01-29 09:56:53

问题


I have two versions of Python on my Mac: 2.7 and 3.8.

When installing packages, I use pip3 instead of pip in order for the packages so install for the correct Python version.

However, I noticed that I can also install with Python3 in the terminal:

 python3 -m pip install ...

Is there a difference between the two? I noticed they showed different pip versions which was a bit strange.


回答1:


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

Using -m is another way of executing an installed module. More specifically, it would be a way of using modules installed for different python versions.

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.




回答2:


python -m pip install ... is the same as using pip, but pip3 is for python3. The only thing you need to change is "python" to "python3" like this: python3 -m pip install ...

Edit: python3 is used when running scripts with python 3.8, while "python" is with python 2.7. pip3 is mainly if you want the libraries installed for python3, and you don't really care about 2.7.




回答3:


This is what you need to know:

  • https://snarky.ca/why-you-should-use-python-m-pip/
  • https://snarky.ca/a-quick-and-dirty-guide-on-how-to-install-packages-for-python/

In short:

Always use the path/to/pythonX.Y -m pip somecommand form, known as the executable module (or executable 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).



来源:https://stackoverflow.com/questions/62765970/what-is-the-difference-between-installing-packages-with-python-3-and-pip3-in-ter

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!