我怎样才能 'clean up' 一个 virtualenv?
How can I 'clean up' a virtualenv?
如果我想让我的 venv 尽可能干净,我该如何清理我不需要的东西?让我举个例子...
说我尝试了一堆新模块...
pip install foo
pip install bar
pip install foobar
pip install foobarfoo
这些模块有自己的一些要求,等等。后来我决定要使用哪个,但后来我的 requirement.txt 里有一大堆东西,我记不起来了我需要什么,不需要什么,取决于什么,等等
如何保持干净整洁?
This答案可能正是您所需要的。
You can install and use the pip-autoremove utility to remove a package
plus unused dependencies.
# install pip-autoremove
pip install pip-autoremove
# remove "somepackage" plus its dependencies:
pip-autoremove somepackage -y
以下对我有用(可以从任何 Python 3.6 virtualenv
执行):
virtualenv --clear your-env-name
其中 your-env-name
可能是:
- 虚拟环境的路径(相对于当前目录或绝对)
- 或者如果您使用
virtualenv-wrapper
,只需使用环境名称
pip uninstall
后跟一个或多个软件包名称将从虚拟环境中删除软件包。
要卸载每个包(包括依赖项),您可以冻结需求,然后将它们传递给 pip uninstall
:
pip freeze > to-uninstall.txt
pip uninstall -r to-uninstall.txt
您可以使用 pip-tools 的 pip-sync
功能来保持环境清洁。
Now that you have a requirements.txt, you can use pip-sync
to update your virtual environment to reflect exactly what's in there. This will install/upgrade/uninstall everything necessary to match the requirements.txt contents.
只需安装 pip-tools 并调用 pip-sync 命令:
pip install pip-tools
pip-sync requirements.txt
(旁注:pip-tools 也非常适合管理您的依赖版本,使您的构建具有可预测性和确定性;有关详细信息,请参阅 pip-tools' documentation)
略微改进
使用 pip 但添加 -y 以避免提示每个库。
完成后不要忘记删除文件“to_uninstall.txt”!
pip freeze > to_uninstall.txt
pip uninstall -y -r to_uninstall.txt
我刚刚为此写了一行:
pip list | tail +3 | cut -f1 -d\ | xargs pip uninstall -y
之后,您可能想要:
python -m ensurepip --upgrade
为了 pip
回来。
pip freeze | xargs pip uninstall -y
(灵感来自 )
关注来自@Steve Rossiter 和@James Rocker 的
也可以做一些微不足道的修改,避免在 one-liner 中生成临时文件,例如:
pip uninstall -y -r <(pip freeze)
(我想 post 这是对答案的评论,但我没有足够的声誉。)
如果我想让我的 venv 尽可能干净,我该如何清理我不需要的东西?让我举个例子...
说我尝试了一堆新模块...
pip install foo
pip install bar
pip install foobar
pip install foobarfoo
这些模块有自己的一些要求,等等。后来我决定要使用哪个,但后来我的 requirement.txt 里有一大堆东西,我记不起来了我需要什么,不需要什么,取决于什么,等等
如何保持干净整洁?
This答案可能正是您所需要的。
You can install and use the pip-autoremove utility to remove a package plus unused dependencies.
# install pip-autoremove pip install pip-autoremove # remove "somepackage" plus its dependencies: pip-autoremove somepackage -y
以下对我有用(可以从任何 Python 3.6 virtualenv
执行):
virtualenv --clear your-env-name
其中 your-env-name
可能是:
- 虚拟环境的路径(相对于当前目录或绝对)
- 或者如果您使用
virtualenv-wrapper
,只需使用环境名称
pip uninstall
后跟一个或多个软件包名称将从虚拟环境中删除软件包。
要卸载每个包(包括依赖项),您可以冻结需求,然后将它们传递给 pip uninstall
:
pip freeze > to-uninstall.txt
pip uninstall -r to-uninstall.txt
您可以使用 pip-tools 的 pip-sync
功能来保持环境清洁。
Now that you have a requirements.txt, you can use
pip-sync
to update your virtual environment to reflect exactly what's in there. This will install/upgrade/uninstall everything necessary to match the requirements.txt contents.
只需安装 pip-tools 并调用 pip-sync 命令:
pip install pip-tools
pip-sync requirements.txt
(旁注:pip-tools 也非常适合管理您的依赖版本,使您的构建具有可预测性和确定性;有关详细信息,请参阅 pip-tools' documentation)
略微改进
使用 pip 但添加 -y 以避免提示每个库。
完成后不要忘记删除文件“to_uninstall.txt”!
pip freeze > to_uninstall.txt
pip uninstall -y -r to_uninstall.txt
我刚刚为此写了一行:
pip list | tail +3 | cut -f1 -d\ | xargs pip uninstall -y
之后,您可能想要:
python -m ensurepip --upgrade
为了 pip
回来。
pip freeze | xargs pip uninstall -y
(灵感来自
关注来自@Steve Rossiter 和@James Rocker 的
pip uninstall -y -r <(pip freeze)
(我想 post 这是对答案的评论,但我没有足够的声誉。)