Usage record of setuptools

Keywords: Python pip

Writing skills of python setup.py file

Environment: the latest version of setuptools. For a preliminary understanding of setuptools, please refer to This article

1. Custom command

from setuptools import setup, Command

class MyCommand(Command):
    description = "Description of the command"
    user_options = []

    # This method must be implemented
    def initialize_options(self):
        pass

    # This method must be implemented
    def finalize_options(self):
        pass

    def run(self):
        print("My command runs!")

setup(..., cmdclass={
    #Commands: inherited classes
    "mycommand": MyCommand
})

The format is like the above. This is the simplest example without custom command sub options. The following is a slightly complex example. Its function is to publish packages to pypi:

import os
from setuptools import setup, Command

class PublishCommand(Command):

    description = "Publish a new version to pypi"

    user_options = [
        # The format is (long option, short option, description).
        ("test", None, "Publish to test.pypi.org"),
        ("release", None, "Publish to pypi.org"),
    ]

    def initialize_options(self):
        """Set default values for options."""
        self.test = False
        self.release = False

    def finalize_options(self):
        """Post-process options."""
        if self.test:
            print("V%s will publish to the test.pypi.org" % version)
        elif self.release:
            print("V%s will publish to the pypi.org" % version)

    def run(self):
        """Run command."""
        os.system("pip install -U setuptools twine wheel")
        os.system("rm -rf build/ dist/ Flask_PluginKit.egg-info/")
        os.system("python setup.py sdist bdist_wheel")
        if self.test:
            os.system("twine upload --repository-url https://test.pypi.org/legacy/ dist/*")
        elif self.release:
            os.system("twine upload dist/*")
        os.system("rm -rf build/ dist/ Flask_PluginKit.egg-info/")
        if self.test:
            print("V%s publish to the test.pypi.org successfully" % version)
        elif self.release:
            print("V%s publish to the pypi.org successfully" % version)
        exit()

setup(..., cmdclass={
    'publish': PublishCommand,
})

The usage of this release command is as follows:

$ python setup.py publish --help
Common commands: (see '--help-commands' for more)

  setup.py build      will build the package underneath 'build/'
  setup.py install    will install the package

Global options:
  --verbose (-v)  run verbosely (default)
  --quiet (-q)    run quietly (turns verbosity off)
  --dry-run (-n)  don't actually do anything
  --help (-h)     show detailed help message
  --no-user-cfg   ignore pydistutils.cfg in your home directory

Options for 'PublishCommand' command:
  --test     Publish to test.pypi.org
  --release  Publish to pypi.org

usage: setup.py [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...]
   or: setup.py --help [cmd1 cmd2 ...]
   or: setup.py --help-commands
   or: setup.py cmd --help

Explanation: with reference to the code and help, publish defines two sub options, test and release. Later run executes the commands uploaded to different environments according to the value of the sub options. Therefore, executing python setup.py publish --test can be published to python official test warehouse test.pypi.org, and executing python setup.py publish --release can be published to python official warehouse pypi.org!

Posted by antileon on Sat, 07 Dec 2019 17:01:47 -0800