How to check the versions of modules installed in Python?

2

I installed two modules in Python via anaconda (conda install):

  • zipfile36 ;

  • MySQLdb .

Using the anaconda prompt I get the version of both and all my other modules using the command:

conda list

But I would like to know, how can I get the version of these modules directly in the code? I have an application that I will need this.

    
asked by anonymous 30.10.2018 / 15:29

2 answers

2

There is no 100% deterministic way for a module to report its version. What there are are conventions and suggestions.

The PEP 8 suggests here that you use a variable of module called __version__ to store the version, however, this is just a suggestion - it is not mandatory for modules to have this variable.

Some modules follow this suggestion and make the version available in __version__ , but others use alternative variables like VERSION or VER or simply version . There are also modules that use a get_version() function capable of generating the dynamically tag-based number of the version control system used.

Fortunately, both MySQLdb and zipfile36 you want use the method suggested by PEP 8. Then you can use:

import zipfile36
print(zipfile36.__version__)

import MySQLdb
print(MySQLdb.__version__)
    
30.10.2018 / 17:43
2

As a matter of curiosity, I saw this SOen response as an alternative to @nosklo's response, where the module is used pkg_resources of setuptools .

import pkg_resources
pkg_resources.get_distribution("requests").version  # nome do pacote no PyPI
# '2.20.0'
    
30.10.2018 / 19:28