Python setuptools 自定义配置
我正在打包一个Python模块,希望用户能够用一些自定义选项来构建这个模块。具体来说,如果用户提供一些特定的可执行文件,包就会进行一些额外的操作。
理想情况下,用户可以运行 setup.py install
或者 setup.py install --magic-doer=/path/to/executable
。如果他们使用第二种方式,我会在代码的某个地方设置一个变量,然后继续处理。
这样做在Python的 setuptools
中可行吗?如果可以,我该怎么做呢?
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的文档关于如何继承Command的内容仍然是空白的,但一个最简单的类看起来会是这样的:
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"]}