[PYTHON] Leverage extras_require in setup.py

background

If you want to create a new package in python, write install_requires in setup.py and

bash


$ python setup.py develop

You will install the dependent packages with.

However, this is inconvenient for managing sphinx, which depends only on document generation, and py.test, which depends only on tests.

Take advantage of extras_require

You can use extras_require to define context-dependent dependent packages.

setup.py


from setuptools import setup

setup(
    name='foo',
    packages=['foo'],
    install_requires=[
        "flask",
    ],
    extras_require = {
        'test': ['pytest'],
        'doc': ['sphinx'],
    },
)

If you use pip to install the package in edit mode, you can also install modules for testing and documentation in the same way as setup.py develop.

bash


$ pip install -e .[test]

reference

bash


$ pip install SomePackage[PDF]
$ pip install SomePackage[PDF]==3.0
$ pip install -e .[PDF]==3.0  # editable project in current directory

Recommended Posts

Leverage extras_require in setup.py