0

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
2
  • Do you mean a list of the modules in the standard library per version? Commented Oct 15, 2016 at 7:46
  • No, I want the inverse. Given a module, list which versions of python have it. Commented Oct 15, 2016 at 7:48

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

What if you get a server error? e.code will then not equal 404 and the function returns true.
@Marichyasana The function does not return true. It just raises the error (that's what the raise e does). When using that function you should check for other errors and just print "server down" or something. That's the downside of a solution that relies on an internet connection...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.