在运行时检查Python模块版本 [英] Checking a Python module version at runtime

查看:79
本文介绍了在运行时检查Python模块版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

许多第三方Python模块都有一个属性,该属性保存该模块的版本信息(通常为module.VERSIONmodule.__version__之类的东西),但是有些则没有.

Many third-party Python modules have an attribute which holds the version information for the module (usually something like module.VERSION or module.__version__), however some do not.

此类模块的特定示例是libxslt和libxml2.

Particular examples of such modules are libxslt and libxml2.

我需要检查这些模块在运行时是否使用了正确的版本.有办法吗?

I need to check that the correct version of these modules are being used at runtime. Is there a way to do this?

一个潜在的解决方案是在运行时读取源,对其进行哈希处理,然后将其与已知版本的哈希进行比较,但这很讨厌.

A potential solution wold be to read in the source at runtime, hash it, and then compare it to the hash of the known version, but that's nasty.

有更好的解决方案吗?

推荐答案

我会远离散列.正在使用的libxslt版本可能包含某种补丁,但不会影响您的使用.

I'd stay away from hashing. The version of libxslt being used might contain some type of patch that doesn't effect your use of it.

作为替代方案,我建议您不要在运行时检查(不知道这是否是一个硬性要求).对于我编写的具有外部依赖性(第3方库)的python东西,我编写了一个脚本,用户可以运行该脚本来检查其python安装,以查看是否安装了适当的模块版本.

As an alternative, I'd like to suggest that you don't check at run time (don't know if that's a hard requirement or not). For the python stuff I write that has external dependencies (3rd party libraries), I write a script that users can run to check their python install to see if the appropriate versions of modules are installed.

对于没有定义版本"属性的模块,您可以检查其包含的接口(类和方法),并查看它们是否与期望的接口匹配.然后,在您正在使用的实际代码中,假设第3方模块具有您期望的接口.

For the modules that don't have a defined 'version' attribute, you can inspect the interfaces it contains (classes and methods) and see if they match the interface they expect. Then in the actual code that you're working on, assume that the 3rd party modules have the interface you expect.

这篇关于在运行时检查Python模块版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆