Checking a Python module version at runtime
Many third-party Python modules have an attribute which holds the version information for the module (usually something like
module.__version__), however some do not.
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.
Is there a better solutions?
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.
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.
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.
Checking a Python module version at runtime, 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. As an alternative PythonServer Side ProgrammingProgramming Use pkg_resources module to get the module version at runtime. Anything installed from PyPI at least should have a version number.
Use pkg_resources. Anything installed from PyPI at least should have a version number.
>>> import pkg_resources >>> pkg_resources.get_distribution("blogofile").version '0.7.1'
How do I get a python module's version number through code , Generalized answer from Matt's, do a dir(YOURMODULE) and look for __version__ , VERSION , or version . Most modules like __version__ but You can use pip to get the versions of python modules. If you want to list all installed Python modules with their version numbers, use the following command: $ pip freeze. You will get the output: asn1crypto==0.22.0 astroid==1.5.2 attrs==16.3.0 Automat==0.5.0 backports.functools-lru-cache==1.3 cffi==1.10.0
- Try checking for functions that exist or don't exist in your needed versions.
- If there are no function differences, inspect function arguments and signatures.
- If you can't figure it out from function signatures, set up some stub calls at import time and check their behavior.
Check the version of Python package / library, How do I check the version of a Python module? To check the version of Python 3 software: python3 ––version. Most systems differentiate Python 2 as python and Python 3 as python3. If you do not have Python 2, your system may use the python command in place of python3. Note: Python does not have a built-in upgrade system.
You can use
to see the installed packages in requirements format.
Check Python version from command line / in script, the information of the installed package with the following command. Execute commands at the command prompt or terminal. In some environments, use pip3 instead of pip . Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. Checking a Python module version at runtime (7
You can use
importlib_metadata library for this.
If you're on python <
3.8, first install it with:
pip install importlib_metadata
3.8 it's included in python's standard library.
Then, to check a package's version (in this example
>>> from importlib_metadata import version >>> version('lxml') '4.3.1'
Keep in mind that this works only for packages installed from PyPI. Also, you must pass a package name as an argument to the
version method, rather than a module name that this package provides (although they're usually the same).
Checking a Python module version at runtime, How do I know what version of Anaconda I have? For more understanding, you can check the complete guide for managing Python modules using pip. If you have the latest version of Python, pip comes preinstalled with Python. Run following commands on the command line (not on Python console). You get the complete list of installed Python modules with their versions.
PyInstaller · PyPI, Question. Many third-party Python modules have an attribute which holds the version information for the module (usually something like module.VERSION or In this guide, you'll look at Python type checking. Traditionally, types have been handled by the Python interpreter in a flexible but implicit way. Recent versions of Python allow you to specify explicit type hints that can be used by different tools to help you develop your code more efficiently.
enforce · PyPI, Check the Python version in the script: sys, platform. You can use the standard library sys module or platform module to get the version of Python Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. Checking a Python module version at runtime. 730. How
How to Check Python Version, (Windows) Allow usage of VSVersionInfo as version argument to EXE again. Fix the test for the “W” run-time Python option to verify module warnings can Check the Python version in the script: sys, platform You can use the standard library sys module or platform module to get the version of Python that is actually running. The content of the script is the same on Windows, Mac, and Linux such as Ubuntu.