Negative extra_requires in Python setup.py
Asked Answered
T

2

7

I'd like to make a Python package that installs a dependency by default unless the user specially signals they do not want that.

Example:

 pip install package[no-django]

Does current pip and setup.py mechanism provide way to do this or does not need to have explicit extra_requires every time?

Tinkling answered 29/4, 2016 at 14:43 Comment(3)
Closest I've found is this: pythonhosted.org/setuptools/…Placative
@Rebs: your link is broken, I think the updated one is here setuptools.readthedocs.io/en/latest/… pardon me if I'm wrongPrefabricate
@Placative that paragraph seems to have moved to the Advertising Behavior section of Setuptools' documentation.Abbie
M
2

I don't think this is possible. A way around it is to do a normal extra requires ... where

install_require=[
    # ...
    # no django listed here
],
extras_require={
    'django': ['django'],
}

and install with package[django] everywhere you need django installed.

Mephitis answered 19/7, 2016 at 17:13 Comment(1)
Yeah it is not possible.Tinkling
M
1

Here is a possible solution. It doesn't use extras_require, but otherwise it satisfies all the stated requirements. With the bellow setup.py file, you can install as following:

# Regular installation
pip install package

# Installation without django
NODJANGO=1
pip install package

setup.py file:

import os
from setuptools import setup

install_requires_without_django = [...]

setup(
    ...
    install_requires=install_requires_without_django  + ([] if os.getenv('NODJANGO', False) else ['django']),,
    ...
)
Myrick answered 8/8, 2021 at 12:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.