6

我正在打包一个 Python 模块,我希望用户能够使用一些自定义选项来构建模块。具体来说,如果您向该软件包提供它可以使用的某些可执行文件,该软件包将发挥一些额外的作用。

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

这可能与 Python 的setuptools吗?如果是这样,我该怎么做?

4

1 回答 1

6

看来您可以...阅读内容。

文章摘录:

命令是从 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"]}
于 2009-12-11T12:16:52.763 回答