distutils: How to pass a user defined parameter to setup.py? distutils: How to pass a user defined parameter to setup.py? python python

distutils: How to pass a user defined parameter to setup.py?


As Setuptools/Distuils are horribly documented, I had problems finding the answer to this myself. But eventually I stumbled across this example. Also, this similar question was helpful. Basically, a custom command with an option would look like:

from distutils.core import setup, Commandclass InstallCommand(Command):    description = "Installs the foo."    user_options = [        ('foo=', None, 'Specify the foo to bar.'),    ]    def initialize_options(self):        self.foo = None    def finalize_options(self):        assert self.foo in (None, 'myFoo', 'myFoo2'), 'Invalid foo!'    def run(self):        install_all_the_things()setup(    ...,    cmdclass={        'install': InstallCommand,    })


Here is a very simple solution, all you have to do is filter out sys.argv and handle it yourself before you call to distutils setup(..).Something like this:

if "--foo" in sys.argv:    do_foo_stuff()    sys.argv.remove("--foo")...setup(..)

The documentation on how to do this with distutils is terrible, eventually I came across this one: the hitchhikers guide to packaging, which uses sdist and its user_options.I find the extending distutils reference not particularly helpful.

Although this looks like the "proper" way of doing it with distutils (at least the only one that I could find that is vaguely documented). I could not find anything on --with and --without switches mentioned in the other answer.

The problem with this distutils solution is that it is just way too involved for what I am looking for (which may also be the case for you).Adding dozens of lines and subclassing sdist is just wrong for me.


Yes, it's 2015 and the documentation for adding commands and options in both setuptools and distutils is still largely missing.

After a few frustrating hours I figured out the following code for adding a custom option to the install command of setup.py:

from setuptools.command.install import installclass InstallCommand(install):    user_options = install.user_options + [        ('custom_option=', None, 'Path to something')    ]    def initialize_options(self):        install.initialize_options(self)        self.custom_option = None    def finalize_options(self):        #print('The custom option for install is ', self.custom_option)        install.finalize_options(self)    def run(self):        global my_custom_option        my_custom_option = self.custom_option        install.run(self)  # OR: install.do_egg_install(self)

It's worth to mention that install.run() checks if it's called "natively" or had been patched:

if not self._called_from_setup(inspect.currentframe()):    orig.install.run(self)else:    self.do_egg_install()

At this point you register your command with setup:

setup(    cmdclass={        'install': InstallCommand,    },    :