python3 -m pip安装VS pip3安装 [英] python3 -m pip install VS pip3 install
问题描述
我总是使用pip install
(我认为它等同于pip3 install
,因为我的环境中只有python3)来安装软件包.但是我最近听说python3 -m pip install
更好.为什么?
I always use pip install
(which I think is equivalent to pip3 install
since I only have python3 in my env) to install packages. But I recently heard python3 -m pip install
is better. Why?
推荐答案
我建议不要直接调用任何pip somecommand
(或pip3
)脚本.相反,显式地为特定的Python解释器调用 pip 的可执行模块更为安全,格式为path/to/pythonX.Y -m pip somecommand
.
I would advise against ever calling any pip somecommand
(or pip3
) script directly. Instead it's much safer to call pip's executable module for a specific Python interpreter explicitly, something of the form path/to/pythonX.Y -m pip somecommand
.
这有很多优点,例如:
- 明确要为哪个Python解释器进行 pip安装(Python 2或3,是否在虚拟环境中,等等)
- 对于虚拟环境,无需激活即可 pip-install (或执行其他操作):
path/to/venv/bin/python -m pip install SomeProject
- 在Windows下,这是唯一可以安全升级 pip 本身
path\to\venv\Scripts\python.exe -m pip install --upgrade pip
的工具
- It is explicit for which Python interpreter the projects will be pip-installed (Python 2 or 3, inside the virtual environment or not, etc.)
- For a virtual environment, one can pip-install (or do other things) without activating it:
path/to/venv/bin/python -m pip install SomeProject
- Under Windows this is the only to safely upgrade pip itself
path\to\venv\Scripts\python.exe -m pip install --upgrade pip
但是,是的,如果一切都完美设置,那么python3 -m pip install SomeProject
和pip3 install SomeProject
应该做完全相同的事情,但是在很多情况下,设置存在问题,并且无法按预期工作并且用户会感到困惑(如该平台上有关此主题的许多问题所示).
But yes, if all is perfectly setup, then python3 -m pip install SomeProject
and pip3 install SomeProject
should do the exact same thing, but there are way too many cases where there is an issue with the setup and things don't work as expected and users get confused (as shown by the many questions about this topic on this platform).
参考
- 布雷特·坎农的文章"为什么要使用
python -m pip
" - pip 的文档部分升级点子"
- venv 的文档创建虚拟环境"部分:"您不需要专门激活环境 [...]"
- Brett Cannon's article "Why you should use
python -m pip
" - pip's documentation section on "Upgrading pip"
- venv's documentation section on "Creating virtual environments": "You don’t specifically need to activate an environment [...]"
这篇关于python3 -m pip安装VS pip3安装的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!