Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 633
rubasov Avatar asked Feb 02 '13 13:02

rubasov


People also ask

What is a standard library in Python?

A standard Python installation comes with a large collection of pre-made modules for many common programming tasks. This collection of modules that is already included when we install Python is known as the ‘standard library’.

How many modules are there in the Python standard library?

:) Besides the official Python documentation, there are multiple web pages where you can find detailed explanations of how to use the modules available in the Python Standard Library. In this article, we have explained a limited number of modules, since the Python Standard Library contains more than 200!

Can I download and install Python modules that somebody else wrote?

We mentioned before that it is possible to download and install modules that somebody else has written. If you install such extra modules, they are added to your Python ‘library’ on your computer, and you can import them just like modules from the standard library.

What is Python 3's standard library?

Python's standard library is very extensive, offering a wide range of functionalities. In this lesson, we will discuss how to use some of Python 3's standard modules such as the statistics module, the math module and the random module. Updated: 04/16/2021


1 Answers

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 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.

like image 133
Martijn Pieters Avatar answered Oct 30 '22 18:10

Martijn Pieters