gpt4 book ai didi

python - 如何以编程方式获取 Python 中已安装模块的版本?

转载 作者:行者123 更新时间:2023-11-28 16:51:18 25 4
gpt4 key购买 nike

对于模块:

required_modules = ['nose', 'coverage', 'webunit', 'MySQLdb', 'pgdb', 'memcache']

和程序:

required_programs = ['psql', 'mysql', 'gpsd', 'sox', 'memcached']

类似于:

# Report on the versions of programs installed
for module in required_modules:
try:
print module.__version__
except:
exit

最佳答案

不幸的是,module.__version__ 并非出现在所有模块中。

解决方法是使用包管理器。当您使用 easy_installpip 安装库时,它会记录已安装的版本。然后你可以这样做:

import pkg_resources
version = pkg_resources.get_distribution("nose").version

关于python - 如何以编程方式获取 Python 中已安装模块的版本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7079735/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com