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

前端 未结 8 976
温柔的废话
温柔的废话 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:23

    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 install
    
    
    class 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,
        },
        :
    

提交回复
热议问题