pip安装选项“忽略安装"之间的区别和“强制重新安装" [英] Difference between pip install options "ignore-installed" and "force-reinstall"

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

问题描述

有两个与重新安装软件包有关的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.

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

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.0重命名为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 know what you're doing 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安装选项“忽略安装"之间的区别和“强制重新安装"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆