問題描述
通過 setup.py
將 Python 控制臺腳本安裝到我的路徑中基本上有兩種方法:
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',
],
}
)
和
setup(
...
scripts = [
'scripts/myscript.sh'
]
)
有什么區(qū)別?我看到第一種方法允許我為我的腳本選擇好的、特定的名稱,但是還有其他區(qū)別嗎?不同的原始用途、兼容性(setuptools、distutils、...?)、用法、...?我很困惑,一個很好的詳細(xì)回復(fù)可以幫助我(可能還有其他人)正確理解這一切.
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.
更新:自從我提出問題 PyPA 發(fā)布 這些關(guān)于該主題的酷文檔.
Update: Since I asked the question PyPA published these cool docs on the topic.
推薦答案
(很棒的)Click 包的文檔 建議幾個理由 使用入口點而不是腳本,包括
The docs for the (awesome) Click package suggest a few reasons to use entry points instead of scripts, including
- 跨平臺兼容性和
- 避免讓解釋器將
__name__
分配給__main__
,這可能導(dǎo)致代碼被導(dǎo)入兩次(如果另一個模塊導(dǎo)入您的腳本)
- cross-platform compatibility and
- avoiding having the interpreter assign
__name__
to__main__
, which could cause code to be imported twice (if another module imports your script)
Click 是實現(xiàn)函數(shù)的好方法,可用作 entry_points
,順便說一句.
Click is a nice way to implement functions for use as entry_points
, btw.
這篇關(guān)于setup.py 中 entry_points/console_scripts 和腳本之間的區(qū)別?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!