解释Python入口点? [英] Explain Python entry points?

查看:49
本文介绍了解释Python入口点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了 Pylons 和 Peak 页面上有关鸡蛋入口点的文档,但我仍然不太明白.有人可以向我解释一下吗?

I've read the documentation on egg entry points in Pylons and on the Peak pages, and I still don't really understand. Could someone explain them to me?

推荐答案

一个入口点"通常是 Python 包的开发人员或用户可能想要使用的函数(或其他可调用的类似函数的对象),尽管也可以提供不可调用的对象作为入口点(如注释中正确指出的那样)!).

An "entry point" is typically a function (or other callable function-like object) that a developer or user of your Python package might want to use, though a non-callable object can be supplied as an entry point as well (as correctly pointed out in the comments!).

最流行的入口点是 console_scripts 入口点,它指向您希望作为命令行工具提供给安装您的软件包的任何人使用的函数.这会进入您的 setup.py 脚本,例如:

The most popular kind of entry point is the console_scripts entry point, which points to a function that you want made available as a command-line tool to whoever installs your package. This goes into your setup.py script like:

entry_points={
    'console_scripts': [
        'cursive = cursive.tools.cmd:cursive_command',
    ],
},

我刚刚部署了一个名为 cursive.tools 的包,我希望它能够提供一个草书"某人可以从命令行运行的命令,例如:

I have a package I've just deployed called cursive.tools, and I wanted it to make available a "cursive" command that someone could run from the command line, like:

$ cursive --help
usage: cursive ...

这样做的方法是定义一个函数,就像文件 cursive/tools/cmd.py 中的 cursive_command 函数,看起来像:

The way to do this is define a function, like maybe a cursive_command function in the file cursive/tools/cmd.py that looks like:

def cursive_command():
    args = sys.argv[1:]
    if len(args) < 1:
        print "usage: ..."

等等;它应该假设它是从命令行调用的,解析用户提供的参数,然后......好吧,执行命令设计的任何操作.

and so forth; it should assume that it's been called from the command line, parse the arguments that the user has provided, and ... well, do whatever the command is designed to do.

安装 docutils 包作为入门示例-point use:它会安装一些类似于将 Python 文档转换为其他格式的有用命令.

Install the docutils package for a great example of entry-point use: it will install something like a half-dozen useful commands for converting Python documentation to other formats.

这篇关于解释Python入口点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆