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?
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.
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.OrderedDictclass. The documentation states it was added in Python 2.7, but a backport is available that works on Python 2.4 and up. Insetup.pyyou test for the presence of the class incollections. If the import fails, add the backport to your requirements list:then in your code where you need
OrderedDictuse the same test:and rely on
piporeasy_installorzc.buildoutor other installation tools to fetch the extra library for you.Many recent core library additions have backports available, including
json(calledsimplejson),argparse,sqlite3(thepysqlitepackage, usefrom pysqlite2 import dbapi as sqlite3as 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.