可以使用get_installed_distributions去列出所有安装的package。
import pip
pip_version = pip.__version__
version = pip_version.split('.')
major = int(version[0])
minor = int(version[1])
if major < 10 and minor == 0:
# pip的版本早于10.0,get_installed_distributions就在pip中。
dists = pip.get_installed_distributions()
elif major >= 21 and minor >= 2:
# pip升级到21.2的版本后,get_installed_distributions已改成
# _get_installed_distributions,并且改到了pip-tools模块中。
from piptools.scripts.sync import _get_installed_distributions
dists = _get_installed_distributions()
else:
# pip升级到10.0之后不能直接get_installed_distributions()
from pip._internal.utils.misc import get_installed_distributions
dists = get_installed_distributions()
dists_list = sorted(["{:<25} {}".format(e.project_name, e.version) for e in dists])
for elem in dists_list:
print(elem)
摘自:
1.?关于pip的cannot import name ‘get_installed_distributions‘问题_yj3058的博客-CSDN博客
2.?Make sure we install an older version of pip to work around a bug by jon-betts · Pull Request #20 · hypothesis/tox-pip-sync · GitHub
3.Python pip.utils 模块,get_installed_distributions() 实例源码 - 编程字典
4.?python报错AttributeError: module ‘pip‘ has no attribute ‘get_installed_distributions‘ 解决_u012724887的专栏-CSDN博客
|