Python pip 安装选项“忽略安装”和“强制重新安装”之间的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51913361/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 19:58:21  来源:igfitidea点击:

Difference between pip install options "ignore-installed" and "force-reinstall"

pythonpippypi

提问by Daniel Chen

There are two pip installoptions related to reinstalling the packages, which are --ignore-installedand --force-reinstall.

有两个pip install与重新安装软件包相关的选项,它们是--ignore-installed--force-reinstall

These two options described as following in the official doc

这两个选项在官方文档中描述如下

--force-reinstall
Reinstall all packages even if they are already up-to-date.

-I, --ignore-installed
Ignore the installed packages (reinstalling instead).

It seems that they all ignore something and do the reinstallation but I cannot tell the difference between them (I can see some difference if I actually execute them ... but I cannot explain). If I search "force reinstall packages in pip", the result lists both --ignore-installedand --force-reinstall, which confuses me for a long time.

似乎他们都忽略了一些东西并重新安装,但我无法区分它们之间的区别(如果我实际执行它们,我可以看到一些区别......但我无法解释)。如果我搜索“在 pip 中强制重新安装软件包”,结果会同时列出--ignore-installed--force-reinstall,这让我困惑了很长时间。

回答by hoefling

--force-reinstall

--force-reinstall

Before installing a package, will uninstall it first if already installed. Pretty much the same as running pip uninstall -y dep && pip install depfor package and its every dependency.

在安装软件包之前,如果已经安装,将首先卸载它。与运行pip uninstall -y dep && pip install dep包及其每个依赖项几乎相同。

--ignore-installed

--ignore-installed

Ignores whether the package and its deps are already installed, overwriting installed files. This means that you can have a situation where --ignore-installeddoes not uninstall a file, leaving it in site-packagesforever. Imagine you have pkgname==1.0that provides module spam:

忽略包及其 deps 是否已安装,覆盖已安装的文件。这意味着您可能会遇到--ignore-installed不卸载文件而将其site-packages永久保留的情况。想象一下,你有pkgname==1.0提供模块spam

$ pip show -f pkgname
Name: pkgname
Version: 1.0
...
spam.py

and the next version pkgname==2.0renamed spamto eggs. When running pip install pkgname==2.0 --ignore-installed, spam.pywill not be removed, left orphaned forever until you remove it manually.

下一个版本pkgname==2.0重命名spameggs. 运行时pip install pkgname==2.0 --ignore-installed,spam.py不会被删除,永远处于孤立状态,直到您手动删除它。

Consequence

结果

--force-reinstallshould always be preferred; use --ignore-installedonly if you know what you're doingare sure that the reinstall will overwrite currently installed files. Otherwise, you may get obscure import errors after reinstall due to stale modules still available in sys.path.

--force-reinstall应该永远是首选;--ignore-installed仅当您知道自己在做什么并确保重新安装将覆盖当前安装的文件时才使用。否则,由于过时的模块在sys.path.

Example

例子

Example to reproduce with the latest pipchanges where all its packages were moved under _internalpackage: create a new virtual environment and downgrade pipto version 9:

使用最新pip更改重现的示例,其中所有包都移到_internal包下:创建新的虚拟环境并降级pip到版本 9:

$ mkvirtualenv testenv
$ workon testenv
(testenv) $ pip install "pip<10"

If you would now upgrade pipto the latest version via --force-reinstall, a clean upgrade is performed. Afterwards, you have the correct package structure with the _internaland _vendor:

如果您现在pip要通过升级到最新版本--force-reinstall,则会执行全新升级。之后,您将拥有正确的包结构_internal_vendor

(testenv) $ pip install pip --upgrade --force-reinstall
(testenv) $ ls -l $VIRTUAL_ENV/lib/python3.7/site-packages/pip
total 16
-rw-r--r--   1 hoefling  staff   21 19 Aug 11:47 __init__.py
-rw-r--r--   1 hoefling  staff  623 19 Aug 11:47 __main__.py
drwxr-xr-x   4 hoefling  staff  128 19 Aug 11:47 __pycache__
drwxr-xr-x  25 hoefling  staff  800 19 Aug 11:47 _internal
drwxr-xr-x  26 hoefling  staff  832 19 Aug 11:47 _vendor

If you would do the upgrade with --ignore-installedinstead:

如果您要使用以下方法进行升级--ignore-installed

(testenv) $ pip install pip --upgrade --ignore-installed
(testenv) $ ls -l $VIRTUAL_ENV/lib/python3.7/site-packages/pip
total 392
-rw-r--r--   1 hoefling  staff     21 19 Aug 12:33 __init__.py
-rw-r--r--   1 hoefling  staff    623 19 Aug 12:33 __main__.py
drwxr-xr-x  14 hoefling  staff    448 19 Aug 12:33 __pycache__
drwxr-xr-x  25 hoefling  staff    800 19 Aug 12:33 _internal
drwxr-xr-x  28 hoefling  staff    896 19 Aug 12:33 _vendor
-rw-r--r--   1 hoefling  staff  11910 19 Aug 12:33 basecommand.py
-rw-r--r--   1 hoefling  staff  10465 19 Aug 12:33 baseparser.py
-rw-r--r--   1 hoefling  staff  16474 19 Aug 12:33 cmdoptions.py
drwxr-xr-x  16 hoefling  staff    512 19 Aug 12:33 commands
drwxr-xr-x   5 hoefling  staff    160 19 Aug 12:33 compat
-rw-r--r--   1 hoefling  staff  32153 19 Aug 12:33 download.py
-rw-r--r--   1 hoefling  staff   8121 19 Aug 12:33 exceptions.py
-rw-r--r--   1 hoefling  staff  39950 19 Aug 12:33 index.py
-rw-r--r--   1 hoefling  staff   5626 19 Aug 12:33 locations.py
drwxr-xr-x   5 hoefling  staff    160 19 Aug 12:33 models
drwxr-xr-x   6 hoefling  staff    192 19 Aug 12:33 operations
-rw-r--r--   1 hoefling  staff  10980 19 Aug 12:33 pep425tags.py
drwxr-xr-x   8 hoefling  staff    256 19 Aug 12:33 req
-rw-r--r--   1 hoefling  staff    156 19 Aug 12:33 status_codes.py
drwxr-xr-x  16 hoefling  staff    512 19 Aug 12:33 utils
drwxr-xr-x   8 hoefling  staff    256 19 Aug 12:33 vcs
-rw-r--r--   1 hoefling  staff  32010 19 Aug 12:33 wheel.py

Upgrading pipwith --ignore-installeddid not uninstall previous package version first, and due to new file structure, new files did not overwrite the old ones. As a consequence, old files are now orphaned and not picked up by any package; even pip uninstall pipwill not remove the orphaned files. One would need to clean them up manually.

升级pip--ignore-installed没有卸载以前的包版本第一,而且由于新的文件结构,新文件没有覆盖旧的。因此,旧文件现在是孤立的,不会被任何包提取;甚至pip uninstall pip不会删除孤立文件。人们需要手动清理它们。

回答by Ismael EL ATIFI

--ignore-installed can also be used if you have a virtual env that inherits the global site-package and you want to override the global installation (without uninstalling it).
For example you can have version N in the global python installation and version N+1 in the venv.
It is very convenient to test/debug a new version of a package in a virtual env.

如果您有一个继承全局站点包的虚拟环境并且您想覆盖全局安装(而不卸载它),也可以使用 --ignore-installed。
例如,您可以在全局 python 安装中使用版本 N,在 venv 中使用版本 N+1。
在虚拟环境中测试/调试包的新版本非常方便。