我正在打包一个 Python 模块,我希望用户能够使用一些自定义选项构建该模块。具体来说,如果您为它提供它可以使用的某些可执行文件,该包将发挥一些额外的作用。
理想情况下,用户将运行 setup.py install
或 setup.py install --magic-doer=/path/to/executable
。如果他们使用第二个选项,我会在代码中的某处设置一个变量,然后从那里开始。
使用 Python 的 setuptools
可以吗?如果可以,我该怎么做?
最佳答案
看来你可以......阅读this .
文章摘录:
Commands are simple class that derives from setuptools.Command, and define some minimum elements, which are:
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
The setuptools doc is still empty about subclassing Command, but a minimal class will look like this:
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
The class can then be hook as a command, using an entry point in its setup.py file:
setup(
# ...
entry_points = {
"distutils.commands": [
"my_command = mypackage.some_module:MyCommand"]}
关于Python setuptools 自定义配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1887641/