我包装了一个Python模块,我想用户能够使用一些自定义的选项来构建模块。具体而言,包会做一些额外的魔法,如果你为它提供一定的可执行文件,它可以使用。

理想情况下,用户将运行setup.py installsetup.py install --magic-doer=/path/to/executable。如果它们使用的第二个选项,我会设置一个变量中的某处代码,并从那里。

这可能与Python的setuptools?如果是这样,我怎么办呢?

有帮助吗?

解决方案

看来你可以...读的

从文章提取物:

  

命令是简单的类,从setuptools.Command导出,并定义某些最少的元件,它们是:

description: describe the command
user_options: a list of options
initialize_options(): called at startup
finalize_options(): called at the end
run(): called to run the command
  

在setuptools的文档仍然是空的有关继承的命令,但最小的类将是这样的:

 class MyCommand(Command):
     """setuptools Command"""
     description = "run my command"
     user_options = tuple()
     def initialize_options(self):
         """init options"""
         pass

     def finalize_options(self):
         """finalize options"""
         pass

     def run(self):
         """runner"""
         XXX DO THE JOB HERE
  

类然后可以钩作为命令,使用入口点在其setup.py文件:

 setup(
     # ...
     entry_points = {
     "distutils.commands": [
     "my_command = mypackage.some_module:MyCommand"]}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top