Python setup.py 并将文件添加到 /bin/

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4840182/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 17:46:10  来源:igfitidea点击:

setup.py and adding file to /bin/

python

提问by Aaron Yodaiken

I can't figure out how to make setup.pyadd a scrip to the the user's /binor /usr/binor whatever.

我无法弄清楚如何使setup.py添加一个纸条给用户/bin/usr/bin或什么的。

E.g., I'd like to add a myscript.pyto /usr/binso that the user can call myscript.pyfrom any directory.

例如,我想添加一个myscript.pyto/usr/bin以便用户可以myscript.py从任何目录调用。

采纳答案by miku

The Python documentation explains it under the installing scriptssection.

Python 文档在安装脚本部分对其进行了解释。

Scripts are files containing Python source code, intended to be started from the command line.

脚本是包含 Python 源代码的文件,旨在从命令行启动。

setup(...,
      scripts=['scripts/xmlproc_parse', 'scripts/xmlproc_val']
)

回答by wh1tney

If you're willing to build and install the entire python package, this is how I would go about it:

如果您愿意构建和安装整个 python 包,我会这样做:

  • Edit the setup() function in setup.py to contain a parameter named scriptsand set its argument as the location of the file(s) you wish to run from anywhere. e.g.
  • 编辑 setup.py 中的 setup() 函数以包含一个名为scripts的参数,并将其参数设置为您希望从任何地方运行的文件的位置。例如

setup(name='myproject',author='',author_email='',scripts=['bin/myscript.py'])

setup(name='myproject',author='',author_email='',scripts=['bin/myscript.py'])

  • Within the directory that contains setup.py, create a bin directory by typing mkdir bin
  • Add myscript.py to this newly-created bindirectory (and make sure it's executable!)
  • cdinto the directory that contains setup.py again, and install the entire python package by typing python setup.py install
  • Once the package is installed, you should be able to run myscript.py from anywhere on the system!
  • 在包含 setup.py 的目录中,通过键入创建一个 bin 目录 mkdir bin
  • 将 myscript.py 添加到这个新创建的bin目录(并确保它是可执行的!)
  • cd再次进入包含 setup.py 的目录,并通过键入安装整个 python 包 python setup.py install
  • 安装包后,您应该能够从系统上的任何位置运行 myscript.py!

回答by DataGreed

Consider using console_scripts:

考虑使用console_scripts

from setuptools import setup
setup(name='some-name',
      ...
      entry_points = {
              'console_scripts': [
                  'command-name = package.module:main_func_name',                  
              ],              
          },
)

Where main_func_nameis a main function in your main module. command-name is a name under which it will be saved in /usr/local/bin/ (usually)

main_func_name主模块中的主函数在哪里。command-name 是一个名称,它将保存在 /usr/local/bin/ 中(通常)

回答by Danny Lessio

There are two ways in order to get a working command line tool from setuptools and PyPI infrastructure:

有两种方法可以从 setuptools 和 PyPI 基础设施中获取工作命令行工具:

  1. The "scripts" Keyword Argument
    This allows the command-line execution of everything you want, it can be a Python script, a shell script or something completely different.
  2. The "console_scripts" Entry Point
    This allows Python functions (not scripts!) to be directly registered as command-line accessible tools.
  1. "scripts" 关键字参数
    这允许命令行执行你想要的一切,它可以是 Python 脚本、shell 脚本或完全不同的东西。
  2. “console_scripts”入口点
    这允许将 Python 函数(不是脚本!)直接注册为命令行可访问工具。