本文介绍了setup.py中的entry_points/console_scripts和脚本之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,有两种方法可以通过setup.py将Python控制台脚本安装到我的路径中:

setup(
    ...
    entry_points = {
        'console_scripts': [
            'foo = package.module:func',
        ],
    }
)

setup(
    ...
    scripts = [
        'scripts/myscript.sh'
    ]
)

有什么区别?我看到第一种方法允许我为脚本选择漂亮的特定名称,但是还有其他区别吗?不同的原始用途,兼容性(setuptools,distutils,...?),用法,...?我很困惑,精心准备的详细答复可以帮助我(可能还有其他人)正确理解所有这一切.

解决方案

(出色)点击包的文档建议使用

使用入口点而不是脚本的一些原因

  1. 跨平台兼容性和
  2. 避免让解释器将__name__分配给__main__,这可能导致代码两次导入(如果另一个模块导入了您的脚本)

单击是实现用作entry_points的功能的好方法,

There are basically two ways to install Python console scripts to my path by setup.py:

setup(
    ...
    entry_points = {
        'console_scripts': [
            'foo = package.module:func',
        ],
    }
)

and

setup(
    ...
    scripts = [
        'scripts/myscript.sh'
    ]
)

What are the differences? I see the first approach allows me to choose nice, specific name for my script, but are there any other differences? Different original purposes, compatibility (setuptools, distutils, ...?), usage, ...? I am quite confused and a nice elaborated reply could help me (and probably also others) to properly understand all this.

解决方案

The docs for the (awesome) Click package suggest a few reasons to use entry points instead of scripts, including

  1. cross-platform compatibility and
  2. avoiding having the interpreter assign __name__ to __main__, which could cause code to be imported twice (if another module imports your script)

Click is a nice way to implement functions for use as entry_points, btw.

这篇关于setup.py中的entry_points/console_scripts和脚本之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 03:58