Skip to content Skip to sidebar Skip to footer

Conditionally Installing Importlib On Python2.6

I have a python library that has a dependency on importlib. importlib is in the standard library in Python 2.7, but is a third-party package for older pythons. I typically keep my

Solution 1:

I don't think this is possible with pip and a single requirements file. I can think of two options I'd choose from:

Multiple requirements files

Create a base.txt file that contains most of your packages:

# base.txt
somelib1
somelib2

And create a requirements file for python 2.6:

# py26.txt
-r base.txt
importlib

and one for 2.7:

# py27.txt
-r base.txt

Requirements in setup.py

If your library has a setup.py file, you can check the version of python, or just check if the library already exists, like this:

# setup.pyfrom setuptools import setup
install_requires = ['somelib1', 'somelib2']

try:
    import importlib
except ImportError:
    install_requires.append('importlib')

setup(
    ...
    install_requires=install_requires,
    ...
)

Post a Comment for "Conditionally Installing Importlib On Python2.6"