【问题标题】:How to autoremove dependent Python packages within a pipenv when uninstalling a package?卸载包时如何在 pipenv 中自动删除依赖的 Python 包?
【发布时间】:2020-10-19 07:03:14
【问题描述】:

当你使用pipenv 安装一个包时,所有依赖的包也会随之安装。使用pipenv uninstall 卸载该软件包不会自动删除相关软件包。

如何在 pipenv 中获得 pip-autoremove 的等效功能?

例如:

$ cd testpipenv
$ pipenv install requests
$ pipenv shell
(testpipenv) $ pipenv graph

requests==2.24.0
 - certifi [required: >=2017.4.17, installed: 2020.6.20]
 - chardet [required: >=3.0.2,<4, installed: 3.0.4]
 - idna [required: >=2.5,<3, installed: 2.10]
 - urllib3 [required: >=1.21.1,<1.26,!=1.25.1,!=1.25.0, installed: 1.25.9]

(testpipenv) $ pipenv uninstall requests
(testpipenv) $ pip list

Package    Version
---------- ---------
certifi    2020.6.20
chardet    3.0.4
idna       2.10
pip        20.1.1
setuptools 47.3.1
urllib3    1.25.9
wheel      0.34.2

requests的依赖包,如urllib3,仍然安装,可以通过验证

(testpipenv) $ python 
>>> import urllib3

这也在这里讨论过:Pipenv uninstall doesn't uninstall dependencies - issue #1470,我还没有找到关于使用 pipenv 自动删除软件包的最新指令集。

使用的版本:

  • pipenv,版本 2020.5.28
  • Python 3.6.10

【问题讨论】:

  • 感谢您的链接,但它不适用于这个问题,我在该问题中专门询问了关于在 pipenv 中执行 pip-autoremove within 的等效操作。不建议使用不考虑 pipenv 的 pip 命令或类似命令。 - 链接pipdeptree 中的另一个命令与pipenv graph 类似,它会显示依赖关系树,但不会自动删除未使用的包。

标签: python python-3.x pip uninstallation pipenv


【解决方案1】:

TL;DR

(testpipenv) $ pipenv uninstall requests && pipenv clean

为了使用pipenvcommands 获得与pip-autoremove 类似的功能,我执行了以下操作,继续上面的示例:

(testpipenv) $ pipenv graph
 
certifi==2020.6.20
chardet==3.0.4
idna==2.10
urllib3==1.25.9

这表明仍然安装了依赖包,但它们已经从 Pipfile.lock 中删除了。因此,使用pipenv clean 将删除它们:

(testpipenv) $ pipenv clean

Uninstalling certifi…
Uninstalling idna…
Uninstalling urllib3…
Uninstalling chardet…

总结...

(testpipenv) $ pipenv uninstall requests && pipenv clean

... 将删除所有依赖包,最接近 pip-autoremove。

这可能比clean 命令“卸载所有未在 Pipfile.lock 中指定的包”更激进如果在使用 clean 之前未将包添加到 Pipfile.lock,则此命令可能会删除超出预期的内容。我不知道这是否真的是一个问题,因为uninstall 会自动更新 Pipfile.lock,除非指定了其他选项,例如 --skip-lock。


另一种选择是......

pipenv --rm && pipenv install

... 这将删除所有内容并基于 Pipfile.lock 重建虚拟环境。这会起作用,但速度很慢,因为它会删除完整的虚拟环境并重新安装所有东西,除了不需要的依赖项。

【讨论】:

    猜你喜欢
    • 2011-11-11
    • 2011-12-16
    • 2015-06-15
    • 1970-01-01
    • 2019-01-03
    • 2021-02-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多