我正在尝试修复我的一个virtualenv -我想将所有已安装的库重置为与生产相匹配的库。
有没有一种快速简单的方法来处理皮普?
我正在尝试修复我的一个virtualenv -我想将所有已安装的库重置为与生产相匹配的库。
有没有一种快速简单的方法来处理皮普?
当前回答
仅使用pip的跨平台支持:
#!/usr/bin/env python
from sys import stderr
from pip.commands.uninstall import UninstallCommand
from pip import get_installed_distributions
pip_uninstall = UninstallCommand()
options, args = pip_uninstall.parse_args([
package.project_name
for package in
get_installed_distributions()
if not package.location.endswith('dist-packages')
])
options.yes = True # Don't confirm before uninstall
# set `options.require_venv` to True for virtualenv restriction
try:
print pip_uninstall.run(options, args)
except OSError as e:
if e.errno != 13:
raise e
print >> stderr, "You lack permissions to uninstall this package.
Perhaps run with sudo? Exiting."
exit(13)
# Plenty of other exceptions can be thrown, e.g.: `InstallationError`
# handle them if you want to.
其他回答
使用virtualenvwrapper函数:
wipeenv
参见wipeenv文档
如果你正在使用pew,你可以使用wipeenv命令:
皮尤wipeenv[环境]
我做到了以下几点:
用当前安装的包列表创建名为reqs.txt的需求文件
pip freeze > reqs.txt
然后卸载reqs.txt中的所有包
# -y means remove the package without prompting for confirmation
pip uninstall -y -r reqs.txt
我喜欢这种方法,因为如果你犯了错误,你总是有一个pip需求文件可以依靠。它也是可重复的,而且是跨平台的(Windows、Linux、MacOs)。
仅使用pip的跨平台支持:
#!/usr/bin/env python
from sys import stderr
from pip.commands.uninstall import UninstallCommand
from pip import get_installed_distributions
pip_uninstall = UninstallCommand()
options, args = pip_uninstall.parse_args([
package.project_name
for package in
get_installed_distributions()
if not package.location.endswith('dist-packages')
])
options.yes = True # Don't confirm before uninstall
# set `options.require_venv` to True for virtualenv restriction
try:
print pip_uninstall.run(options, args)
except OSError as e:
if e.errno != 13:
raise e
print >> stderr, "You lack permissions to uninstall this package.
Perhaps run with sudo? Exiting."
exit(13)
# Plenty of other exceptions can be thrown, e.g.: `InstallationError`
# handle them if you want to.
就我而言,我意外地在macOS上使用homebrew安装的pip在全球范围内安装了许多软件包。恢复到默认包的最简单方法是:
$ brew reinstall python
或者,如果你使用pip3:
$ brew reinstall python3