Determining if a given Python module is part of the standard library

How to determine if a Python module is part of the standard library? In other words: is there a Python equivalent for a Perl-compatible utility?

I would use this to justify portability during development. If it depends on the implementation, I'm interested in CPython.

The best answer I've found so far is this:

What parts of the python standard library will 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 using the utility, and also does not say anything about the minimum required versions.

+5
source share
2 answers

When using setuptools install script ( setup.py), you check the required module and update the list of installation dependencies to add backports if necessary.

For example, let's say you need a class collections.OrderedDict. The documentation states that it was added in Python 2.7, but the backport is available nofollow noreferrer is available →, which works on Python 2.4 and higher. In setup.pyyou check for class c collections. If import is not possible, add back to the list of requirements:

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 OrderedDictuse the same test:

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

and rely on pipeither easy_installor zc.buildoutor other installation tools to get an extra library for you.

backports, json ( simplejson), argparse, sqlite3 ( pysqlite, from pysqlite2 import dbapi as sqlite3 ).

; Python , , , , , Python .

+9

. , , . ? , , , , .

, . , , , Python .

: .

0

All Articles