在 setup.py 中入口点/控制台脚本和脚本之间的区别?

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

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

还有

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

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

更新: 由于我提出的问题 PyPA发表了 这些关于这个话题的很酷的文档

35586 次浏览

One key difference between these two ways of creating command line executables is that with the setuptools approach (your first example), you have to call a function inside of the script -- in your case this is the func inside of your module. However, in the distutils approach (your second example) you call the script directly (which allows being listed with or without an extension).

The setup tools entry point approach (#1) also has the benefit that on windows an .exe will be created that can be double clicked and invoked like a regular windows program. This is in addition to having a script placed in the bin path on posix-like systems.

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.

One more difference is that when using console_scripts, my module's __init__ file was run. When just using scripts, the module __init__ was not run, only the script was run.