天天看點

python打包分發工具setuptoo

關于python setup.py檔案的編寫技巧

環境:最新版setuptools,初步認識setuptools可以參考這篇文章

1. 自定義指令

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={
    #"指令": 繼承類
    "mycommand": MyCommand
})           

複制

格式大概是上面這樣了,這是一個沒有自定義指令子選項的最簡單例子,下面是一個稍微複雜的例子,它的作用是将包釋出到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,
})           

複制

這個釋出指令使用方法是:

$ 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           

複制

解釋:參考代碼和幫助,publish定義了兩個子選項,test和release,後面run根據判斷子選項值來執行上傳到不同環境的指令,是以執行

python setup.py publish --test

可以釋出到python官方測試倉庫test.pypi.org,執行

python setup.py publish --release

可以釋出到python官方正式倉庫pypi.org!