简体   繁体   English

Python 命令行脚本中“script”与“entry_point”的优缺点

[英]Pros and cons of 'script' vs. 'entry_point' in Python command line scripts

Python's setuptool has two ways of adding command line scripts to a Python package: script and entry_point . Python 的setuptool有两种向 Python 包添加命令行脚本的方法: scriptentry_point

This tutorial outlines these ways: 本教程概述了这些方法:

scripts

Add a Python script ( funniest-joke ) to the package tree, and add its path to setup.py :将 Python 脚本 ( funniest-joke ) 添加到包树,并将其路径添加到setup.py

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

Entry point:入口点:

Add a Python script ( funniest-joke ) to the package tree.将 Python 脚本 ( funniest-joke ) 添加到包树中。 Add a main() function to it, and add command_line.py submodule which runs funniest's main() :向其添加main()函数,并添加运行最有趣的main() command_line.py子模块:

command_line.py : command_line.py :

import funniest

def main():
    print funniest.joke()

setup.py

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

What are the advantages and disadvantages of each method?每种方法的优缺点是什么?

Basically scripts is the old way which requires you to have a stand-alone, executable script file and the entry-points method lets you define which functions you want to run when a command is given.基本上,脚本是旧方法,它要求您拥有一个独立的、可执行的脚本文件,并且入口点方法允许您定义在给出命令时要运行的函数。 This way you can have several functions in the same file/module and then have 'entry points' which will be called when the user types in one of the console_scripts commands.通过这种方式,您可以在同一个文件/模块中拥有多个函数,然后拥有“入口点”,当用户键入其中一个console_scripts命令时,将调用该入口点。

Although setup() supports a scripts keyword for pointing to pre-made scripts to install, the recommended approach to achieve cross-platform compatibility is to use console_scripts entry points (see below).尽管 setup() 支持用于指向要安装的预制脚本的脚本关键字,但实现跨平台兼容性的推荐方法是使用 console_scripts 入口点(见下文)。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM