【问题标题】:Pros and cons of 'script' vs. 'entry_point' in Python command line scriptsPython 命令行脚本中“脚本”与“入口点”的优缺点
【发布时间】:2014-06-13 00:16:48
【问题描述】:

Python 的setuptool 有两种向 Python 包添加命令行脚本的方法:scriptentry_point

This tutorial 概述了这些方式:

scripts

将 Python 脚本 (funniest-joke) 添加到包树中,并将其路径添加到 setup.py

setup(
    ...
    scripts=['bin/funniest-joke'],
    ...
)

入口点:

将 Python 脚本 (funniest-joke) 添加到包树中。添加main() 函数,并添加command_line.py 运行最有趣的main() 子模块:

command_line.py:

import funniest

def main():
    print funniest.joke()

setup.py

setup(
    ...
    entry_points = {
        'console_scripts': ['funniest-joke=funniest.command_line:main'],
    }
    ...
)

每种方法的优缺点是什么?

【问题讨论】:

标签: python scripting setuptools setup.py


【解决方案1】:

基本上,脚本是要求您拥有独立的可执行脚本文件的旧方法,而入口点方法可让您定义在给出命令时要运行哪些函数。这样,您可以在同一个文件/模块中拥有多个函数,然后在用户键入console_scripts 命令之一时调用“入口点”。

虽然 setup() 支持脚本关键字来指向要安装的预制脚本,但实现跨平台兼容性的推荐方法是使用 console_scripts 入口点(见下文)。

来自https://packaging.python.org/tutorials/distributing-packages/#scripts (old source)

【讨论】:

    猜你喜欢
    • 2020-04-28
    • 2011-02-06
    • 1970-01-01
    • 1970-01-01
    • 2022-07-14
    • 1970-01-01
    • 1970-01-01
    • 2011-04-21
    • 2015-08-18
    相关资源
    最近更新 更多