sisanared
sisanared

Reputation: 4287

Python - utility program to check module availability

Is there a command line(preferably) utility program that lists all major versions of python that come bundled with a particular module in that version's library?

For example json module is only available in versions 2.6+

So running this utility should list:

2.6
2.7
.. and the 3.x versions

Upvotes: 0

Views: 74

Answers (1)

Bakuriu
Bakuriu

Reputation: 101929

There is no such built-in command.

If having an internet connection is not an issue you can simply check the url at https://docs.python.org/<version-number>/library/<module-name>.html:

from urllib.request import urlopen
from urllib.error import HTTPError


def has_module(version, module_name):
    try:
        urlopen('https://docs.python.org/{}/library/{}.html'.format(version, module_name))
    except HTTPError as e:
        if e.code == 404:
            return False
        raise e
    return True

Example usage:

In [2]: has_module('2.7', 'asyncio')
Out[2]: False

In [3]: has_module('3.3', 'asyncio')
Out[3]: False

In [4]: has_module('3.4', 'asyncio')
Out[4]: True

Must be run in python3, but a python2 version isn't hard to write.

If you want an offline tool you can always parse the page https://docs.python.org/<version-number>/library/index.html and obtain a list of module pages for that python version and then just build a dict that maps the python version to the set of available modules.

Upvotes: 1

Related Questions