卸载包时如何自动删除pipenv中的依赖Python包?

Chr*_*isW 4 python pip uninstallation python-3.x pipenv

当你pipenv用来安装一个包时,所有的依赖包也将随它一起安装。卸载该包pipenv uninstall不会自动删除依赖包。

如何获得pip-autoremovepipenv中的等效功能?

例如:

$ 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

Run Code Online (Sandbox Code Playgroud)

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

(testpipenv) $ python 
>>> import urllib3
Run Code Online (Sandbox Code Playgroud)

这也在这里讨论过:Pipenv 卸载不会卸载依赖项 - 问题 #1470,而且我还没有找到关于使用 pipenv 自动删除软件包的最新指令集。

使用的版本:

  • pipenv,版本 2020.5.28
  • 蟒蛇 3.6.10

Chr*_*isW 7

TL; 博士

(testpipenv) $ pipenv uninstall requests && pipenv clean
Run Code Online (Sandbox Code Playgroud)

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

(testpipenv) $ pipenv graph
 
certifi==2020.6.20
chardet==3.0.4
idna==2.10
urllib3==1.25.9
Run Code Online (Sandbox Code Playgroud)

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

(testpipenv) $ pipenv clean

Uninstalling certifi…
Uninstalling idna…
Uninstalling urllib3…
Uninstalling chardet…
Run Code Online (Sandbox Code Playgroud)

总之...

(testpipenv) $ pipenv uninstall requests && pipenv clean
Run Code Online (Sandbox Code Playgroud)

... 将删除所有依赖包,是最接近的等价于pip-autoremove.

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


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

pipenv --rm && pipenv install
Run Code Online (Sandbox Code Playgroud)

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