Determining if a given Python module is part of the standard library
Question: How can I determine whether a Python module is part of the standard library?
In other words: is there a Python equivalent of perl's corelist utility?

I would use this to set my expectations on portability during development. In
case it's implementation dependent, I'm interested in CPython.

The best answer I found so far is this:

[Which parts of the python standard library are guaranteed to be
available?](http://stackoverflow.com/questions/13334536/which-parts-of-the-
python-standard-library-are-guaranteed-to-be-available)

That is to search for the module name on the index page of the Python Standard
Library Documentation: <http://docs.python.org/2/library/>. However, this is
less convenient than having a utility and also does not tell me anything about
minimally required versions.


Answer: When using a setuptools install script (`setup.py`), you _test_ for the
required module, and update the installation dependencies list to add
backports if needed.

For example, say you need the `collections.OrderedDict` class. The
documentation states it was added in Python 2.7, but a [backport is
available](http://pypi.python.org/pypi/ordereddict) that works on Python 2.4
and up. In `setup.py` you test for the presence of the class in `collections`.
If the import fails, add the backport to your requirements list:

    
    
    from setuptools import setup
    
    install_requires = []
    
    try:
        from collections import OrderedDict
    except ImportError:
        install_requires.append('ordereddict')
    
    setup(
        # ...
        install_requires=install_requires
    )
    

then in your code where you need `OrderedDict` use the same test:

    
    
    try:
        from collections import OrderedDict
    except ImportError:
        # use backported version
        from ordereddict import OrderedDict
    

and rely on `pip` or `easy_install` or `zc.buildout` or other installation
tools to fetch the extra library for you.

Many recent core library additions have backports available, including `json`
(called `simplejson`), `argparse`, `sqlite3` (the `pysqlite` package, use
`from pysqlite2 import dbapi as sqlite3` as a fallback).

You'll _still_ have to read the documentation; the Python documentation is
_excellent_ , and for new modules, classes, methods, functions or arguments
the documentation mentions explicitly in what Python version they were added.

