[英]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"; 在过去,我见过的大多数关于
distutils
或setuptools
似乎都是为了分发所谓的“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
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.