Python setuptools 自定义配置

发布于 2024-08-14 04:11:33 字数 294 浏览 10 评论 0原文

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

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

这可以通过 Python 的 setuptools 实现吗?如果是这样,我该怎么做?

I'm packaging up a Python module, and I would like users to be able to build the module with some custom options. Specifically, the package will do some extra magic if you provide it with certain executables that it can use.

Ideally, users would run setup.py install or setup.py install --magic-doer=/path/to/executable. If they used the second option, I’d set a variable somewhere in the code, and go from there.

Is this possible with Python's setuptools? If so, how do I do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

私藏温柔 2024-08-21 04:11:33

看来你可以...阅读这个< /a>.

文章摘录:

Commands 是从 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"]}

It seems you can... read this.

Extract from article:

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"]}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文