python-packaging 命令行腳本

https://python-packaging-zh.readthedocs.io/zh_CN/latest/command-line-scripts.html#scripts

setuptools可以將命令行工具添加到包中.有兩種方法:

1.scripts參數
把命令工具卸載單獨的文件中,一般放在bin目錄下

test/
    test/
        __init__.py
        ...
    setup.py
    bin/
        test-test
    ...

setup.py中添加

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

2.console_scripts
console_scripts是一個’entry points,允許python的一個def註冊成命令行工具.

test/
    test/
        __init__.py
        command_line.py
        ...
    setup.py
    ...

修改command_line.py模塊只提供命令行工具:

import test

def main():
    print test.aa()

setup.py中註冊main()

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章