python打包分发工具setuptools使用记录

 开发  python  setuptools  setup   2018-09-21 16:18   评论次 

关于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!


点赞 打赏
取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

交流分享

QQ扫一扫或点击二维码加入,群号577543189
微信扫一扫,手滑关注它
微信扫一扫,进入小程序

推荐文章

标签云集

友情链接  交换友链

推广位