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

查看:31
本文介绍了在运行时检查 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.

对于没有定义version"属性的模块,你可以检查它包含的接口(类和方法),看看它们是否与他们期望的接口匹配.然后在您处理的实际代码中,假设第 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天全站免登陆