requirements.txt depending on python version

You can create multiple requirements files, put those common packages in a common file, and include them in another pip requirements file with -r file_path

requirements/
  base.txt
  python2.txt
  python3.txt

python2.txt:

-r base.txt
Django==1.4 #python2 only packages

python3.txt:

-r base.txt
Django==1.5 #python3 only packages

pip install -r requirements/python2.txt


You can use the environment markers to achieve this in requirements.txt since pip 6.0:

SomeProject==5.4; python_version < '2.7'
SomeProject; sys_platform == 'win32'

It is supported by setuptools too by declaring extra requirements in setup.py:

setup(
    ...
    install_requires=[
        'six',
        'humanize',
    ],
    extras_require={
        ':python_version == "2.7"': [
            'ipaddress',
        ],
    },
)

See also requirement specifiers. And Strings for the string versions of corresponding Python commands.

Tags:

Python

Pip