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

前端 未结 8 898
温柔的废话
温柔的废话 2020-11-30 21:32

Please prompt me how to pass a user-defined parameter both from the command line and setup.cfg configuration file to distutils\' setup.py script. I want to write a setup.py

相关标签:
8条回答
  • 2020-11-30 22:31

    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, Command
    
    class 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,
        }
    )
    
    0 讨论(0)
  • 2020-11-30 22:35

    You can't really pass custom parameters to the script. However the following things are possible and could solve your problem:

    • optional features can be enabled using --with-featurename, standard features can be disabled using --without-featurename. [AFAIR this requires setuptools]
    • you can use environment variables, these however require to be set on windows whereas prefixing them works on linux/ OS X (FOO=bar python setup.py).
    • you can extend distutils with your own cmd_classes which can implement new features. They are also chainable, so you can use that to change variables in your script. (python setup.py foo install) will execute the foo command before it executes install.

    Hope that helps somehow. Generally speaking I would suggest providing a bit more information what exactly your extra parameter should do, maybe there is a better solution available.

    0 讨论(0)
提交回复
热议问题