【问题标题】:How can one enable a shell command line argument for a python package installed for pip?如何为 pip 安装的 python 包启用 shell 命令行参数?
【发布时间】:2016-04-29 09:50:07
【问题描述】:

那么对于 python 包,如何从 pip 安装包中启用命令行参数?下面是一个名为 mrbob 的包的示例:

$ pip install mrbob
$ mrbob DoSomethingFunction

如何为 python 包启用命令行选项和新的 shell 命令?

【问题讨论】:

  • 如果包不支持此功能,您必须编写自己的 CLI 包装器。
  • mrbob 必须添加到系统路径

标签: python pip pypi


【解决方案1】:

使用setuptools,定义一个console_script 入口点:

setup(
    # other arguments here...
    entry_points={
        'console_scripts': [
            'mrbob = my_package.some_module:main_func',
        ]
    }
)

有关详细信息,请参阅 setuptools 文档中的 automatic script creation

使用distutils,使用scripts

setup(...,
      scripts=['scripts/mrbob']
      )

有关更多详细信息,请参阅 distutils 文档中的 installing scripts

【讨论】:

    【解决方案2】:

    mrbob 将安装在bin 目录中 例如就我而言,它位于:

    /Library/Frameworks/Python.framework/Versions/2.7/bin/

    只要这是在你的$PATH 你就很好。

    【讨论】:

    • “your”、“you”和“are”真的很难打出来吗?
    猜你喜欢
    • 2017-04-19
    • 1970-01-01
    • 2017-02-23
    • 2015-08-12
    • 2019-12-18
    • 2017-04-18
    • 2022-08-16
    • 2014-12-31
    • 2022-12-25
    相关资源
    最近更新 更多