Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 dependencies in a pip-style requirements.txt. Of course, if I put importlib in here, it will fail if installed on 2.7. How can I conditionally install importlib only if it's not available in the standard lib?

like image 585
David Eyk Avatar asked Feb 23 '12 17:02

David Eyk


1 Answers

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.py
from setuptools import setup
install_requires = ['somelib1', 'somelib2']

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

setup(
    ...
    install_requires=install_requires,
    ...
)
like image 130
jterrace Avatar answered Sep 21 '22 05:09

jterrace