本文介绍了pip 安装选项“ignore-installed"之间的区别和“强制重新安装"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

There are two pip install options related to reinstalling the packages, which are --ignore-installed and --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).

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

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-installed and --force-reinstall, which confuses me for a long time.

推荐答案

--force-reinstall

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

--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 dep for package and its every dependency.

忽略包及其 deps 是否已经安装,覆盖已安装的文件.这意味着您可能会遇到 --ignore-installed 不会卸载文件的情况,而是将其永远留在 site-packages 中.假设您有 pkgname==1.0 提供模块 spam:

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

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

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

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

--force-reinstall 应该是首选;使用 --ignore-installed 仅当您 确定重新安装将覆盖当前安装的文件时.否则,由于 sys.path 中仍然可用的过时模块,您可能会在重新安装后遇到模糊的导入错误.

--force-reinstall should always be preferred; use --ignore-installed only if you are 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.

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

Example to reproduce with the latest pip changes where all its packages were moved under _internal package: create a new virtual environment and downgrade pip to version 9:

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

如果您现在要通过 --force-reinstallpip 升级到最新版本,则会执行干净升级.之后,您将拥有带有 _internal_vendor 的正确包结构:

If you would now upgrade pip to the latest version via --force-reinstall, a clean upgrade is performed. Afterwards, you have the correct package structure with the _internal and _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

如果您使用 --ignore-installed 进行升级:

If you would do the upgrade with --ignore-installed instead:

(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

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

Upgrading pip with --ignore-installed did 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 pip will not remove the orphaned files. One would need to clean them up manually.

这篇关于pip 安装选项“ignore-installed"之间的区别和“强制重新安装"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 23:50