【问题标题】:Get extra argument passed on the installation获取安装时传递的额外参数
【发布时间】:2018-03-16 17:22:09
【问题描述】:

与这个问题有关: Specify extras_require with pip install -e

有什么方法可以让用户以额外的编程方式传递参数?

即当一些用户执行时:

pip install pkg[extra]

我想捕获用户在setup.py 中提出的extra 参数并执行某些操作。

【问题讨论】:

    标签: python


    【解决方案1】:

    extra_require 并不是真的应该这样使用,它只是指定了一个依赖项。

    如果你想提供一个自定义选项,你应该写这样的东西

    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,
        },
    )
    

    然后可以用作

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

    或使用点子

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

    link, link

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-15
      • 2012-06-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-27
      • 2017-06-01
      相关资源
      最近更新 更多