Conditionally installing importlib on python2.6

拈花ヽ惹草 提交于 2019-12-10 14:42:34

问题


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?


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

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

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


来源:https://stackoverflow.com/questions/9418064/conditionally-installing-importlib-on-python2-6

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!