gpt4 book ai didi

python - 检查未定义 __version__ 的 Python 库版本

转载 作者:太空宇宙 更新时间:2023-11-03 13:05:16 25 4
gpt4 key购买 nike

我正在处理一个未定义 __version__ 变量 ( sqlalchemy-migrate ) 的 Python 库,我希望根据我安装的库版本在我的代码中有不同的行为。

有没有办法在运行时检查安装了哪个版本的库(除了检查 pip freeze 的输出)?

最佳答案

这是 Python,接受的方法通常是在库中调用一些行为根据您安装的版本而有所不同的东西,比如:

import somelibrary
try:
somelibrary.this_only_exists_in_11()
SOME_LIBRARY_VERSION = 1.1
except AttributeError:
SOME_LIBRARY_VERSION = 1.0

更优雅的方法可能是创建包装函数。

def call_11_feature():
try:
somelibrary.this_only_exists_in_11()
except AttributeError:
somelibrary.some_convoluted_methods()
somelibrary.which_mimic()
somelibrary.the_11_feature()

关于python - 检查未定义 __version__ 的 Python 库版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4996647/

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