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.