我正在打包一个Python模块,我希望用户能够使用一些自定义选项构建模块.具体来说,如果你为它提供了可以使用的某些可执行文件,它将会做一些额外的魔术.
理想情况下,用户会运行setup.py install或setup.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
Run Code Online (Sandbox Code Playgroud)
关于子类化Command,setuptools doc仍然是空的,但是一个最小的类看起来像这样:
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
Run Code Online (Sandbox Code Playgroud)
然后可以使用setup.py文件中的入口点将该类挂钩为命令:
setup(
# ...
entry_points = {
"distutils.commands": [
"my_command = mypackage.some_module:MyCommand"]}
Run Code Online (Sandbox Code Playgroud)