简体   繁体   English

我如何使用setuptools或distutils来分发脚本而不是Python包?

[英]How do I use setuptools or distutils to distribute a script as opposed to a Python package?

In the past, most tutorials that I've seen of distutils or setuptools seem geared toward distributing so-called "Python packages"; 在过去,我见过的大多数关于distutilssetuptools似乎都是为了分发所谓的“Python包”; ie, collections of closely related Python modules, which are ultimately intended to be imported together as a unit into something else. 即,密切相关的Python模块的集合,最终旨在作为一个单元一起导入到其他东西中。

I have something slightly different: a large command line script. 我有一些略有不同:一个大的命令行脚本。 The script currently lives in a Python project with a structure that looks similar to this: 该脚本目前位于Python项目中,其结构与此类似:

git_repo/
    .gitignore
    README.rst
    src/
        myscript.py
        sub_module1.py
        sub_module2.py
        sub_module3.py
    test/
        test_sub_module1.py
        test_sub_module2.py
        test_sub_module3.py

Inside of the myscript.py file, the first several lines look like this: myscript.py文件中,前几行看起来像这样:

#!/usr/bin/env python

import sub_module1
import sub_module2
import sub_module3

# Go do a bunch of stuff...
# etc...

The code contained in the various submodules is mostly stuff that I couldn't imagine ever wanting to re-use in another project; 包含在各个子模块中的代码主要是我无法想象在另一个项目中重复使用的东西; those pieces are all fairly specific to the main application, myscript.py . 这些部分都非常特定于主应用程序myscript.py In addition, the material inside of each submodule.py file is also not all that closely related between one submodule and another. 此外,每个submodule.py文件中的内容也不是一个子模块与另一个子模块之间密切相关的内容。 Collectively, I don't think that it would be particularly natural or logical to group them together into a sub-package with its own __init__.py file. 总的来说,我不认为将它们组合成一个带有自己的__init__.py文件的子包是特别自然或合乎逻辑的。 In fact, the only reason I even have these submodule files in the first place is simply to help organize the main script more cleanly; 事实上,我首先拥有这些子模块文件的唯一原因就是帮助组织主要脚本更干净; doing it that way results in the top-level myscript.py file coming out to, say, 100 lines, rather than all piled together into a single massive 1000 line scroll. 这样做会导致顶级的myscript.py文件出现,例如100行,而不是全部堆积成一个巨大的1000行滚动。

Within my git_repo/src directory, I can execute this script by typing it out at the command line, eg: 在我的git_repo/src目录中,我可以通过在命令行输入来执行此脚本,例如:

./myscript.py --opt1 <value_1> --opt2 <value_2> --opt3 <value_3> ...

My question: since this project is a command line script and not an importable package, how should I call the setuptools setup() function in that case? 我的问题:由于这个项目是一个命令行脚本而不是一个可导入的包,在这种情况下如何调用setuptools setup()函数? How do I select the input parameters to setup() in order to let it know that it should treat myscript.py as an executable script (meaning, for example, that it knows to do chmod 755 myscript.py during installation), while also making clear that the accompanying submodule.py files, although not scripts themselves, are nevertheless required dependencies which should be installed adjacent to myscript.py within the same directory? 我如何选择setup()的输入参数,以便让它知道它应该将myscript.py视为可执行脚本(例如,它意味着它知道在安装期间执行chmod 755 myscript.py ),同时也明确说明附带的submodule.py文件虽然不是脚本本身,但仍然需要依赖应该安装在同一目录下的myscript.py附近? What is the correct form of the setup function in that case? 在这种情况下, setup功能的正确形式是什么?

You should use setuptools entry points and pip install pkg will create a bin/ script for you. 你应该使用setuptools入口点和pip install pkg将为你创建一个bin/ script。 When you do a system-wide package installation the script will go to /usr/bin or /usr/local/bin . 执行系统范围的软件包安装时,脚本将转至/usr/bin/usr/local/bin

About entry points 关于入口点

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Gnome 中分发 Python 包的 `.desktop` 文件和图标(使用 distutils 或 setuptools)? - How to distribute `.desktop` files and icons for a Python package in Gnome (with distutils or setuptools)? 如何使用setuptools创建包含多个模块的python包? - How do I use setuptools to create a python package with multiple modules? 如何在不使用distutils的情况下创建一个debian软件包来分发python模块? - how can I create a debian package to distribute python modules without using distutils? 使用setuptools / distribute创建python包时,是否需要* .egg-info目录? - Do I need *.egg-info directories when using setuptools/distribute to create a python package 分发、distutils、setuptools 和 distutils2 之间的区别? - Differences between distribute, distutils, setuptools and distutils2? 如何在 setuptools/distutils 中包含 package 数据? - How to include package data with setuptools/distutils? 如何解决未完全安装的软件包(python3-setuptools)? - How do I resolve not fully installed package (python3-setuptools)? 通过setuptools或distutils安装Python软件包后,如何访问可编辑的配置文件? - How to access an editable configuration file once a Python package has been installed via setuptools or distutils? 使用distribute / setuptools创建符号链接(或运行脚本)? - Use distribute/setuptools to create symlink (or run script)? 用于GO源中`extra`包的Python setuptools / distutils自定义构建 - Python setuptools/distutils custom build for the `extra` package from GO source
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM