简体   繁体   中英

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"; ie, collections of closely related Python modules, which are ultimately intended to be imported together as a unit into something else.

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:

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:

#!/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 . In addition, the material inside of each submodule.py file is also not all that closely related between one submodule and another. 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. 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.

Within my git_repo/src directory, I can execute this script by typing it out at the command line, eg:

./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? 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? What is the correct form of the setup function in that case?

You should use setuptools entry points and pip install pkg will create a bin/ script for you. When you do a system-wide package installation the script will go to /usr/bin or /usr/local/bin .

About entry points

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM