4

Related to this question: Specify extras_require with pip install -e

There is some way to get the argument passed by the user as extra programmatically?

i.e. when some user execute:

pip install pkg[extra]

I would like to capture that extra argument that the user has put and do something in setup.py.

mmngreco
  • 516
  • 5
  • 14

1 Answers1

1

extra_require is not really supposed to be used this way, it only specifies a dependency.

If you want to provide a custom option, you should write something like this

from setuptools import setup
from setuptools.command.install import install

class InstallCommand(install):
    user_options = install.user_options + [
        ('someopt', None, None),    # a 'flag' option
        ('someval=', None, None)    # an option that takes a value
    ]

    def initialize_options(self):
        install.initialize_options(self)
        self.someopt = None
        self.someval = None

    def finalize_options(self):
        super(InstallCommand, self).finalize_options()
        assert self.someopt
        assert self.someval == 'asdf'

setup(
    name="pkg",

    cmdclass={
        'install': InstallCommand,
    },
)

which can then be used as

./setup.py install --someopt --someval=asdf

or with pip

pip install pkg --install-option='--someopt' --install-option='--someval=asdf'

link, link

pacholik
  • 8,607
  • 9
  • 43
  • 55