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

python打包分发工具setuptools使用记录的更多相关文章

  1. Python打包分发工具setuptools

    作为Python标准的打包及分发工具,setuptools可以说相当地简单易用.它会随着Python一起安装在你的机器上.你只需写一个简短的setup.py安装文件,就可以将你的Python应用打包 ...

  2. Python打包分发工具setuptools简介(转)

    作为Python标准的打包及分发工具,setuptools可以说相当地简单易用.它会随着Python一起安装在你的机器上.你只需写一个简短的setup.py安装文件,就可以将你的Python应用打包. ...

  3. Python包管理工具setuptools详解及entry point

    1.什么是setuptools? setuptools是Python distutils增强版的集合,它可以帮助我们更简单的创建和分发Python包,尤其是拥有依赖关系的.用户在使用setuptool ...

  4. Python包管理工具setuptools之setup函数参数详解

    **********************************************************对所学内容的简单汇总******************************** ...

  5. Python包管理工具setuptools相关

    setup函数常用参数: --name                         包名称 --version                      包版本 --author          ...

  6. Python 打包中 setpy.py settuptools pbr 的了解

    背景 nova服务构建失败,报错: 'tests_require' must be a string or list of strings containing valid project/versi ...

  7. python打包工具distutils、setuptools分析

    在上一篇博文中总结了python中导入包,安装包一条完整的线路.其中有一个有意思的知识点,安装包的方式有很多种,模块和包管理中打包,发布,安装也是值得研究的内容. python中安装包的方式有很多种: ...

  8. python打包工具distutils、setuptools的使用

    python中安装包的方式有很多种: 源码包:python setup.py install 在线安装:pip install 包名(linux) / easy_install 包名(window) ...

  9. Python打包工具setuptools的使用

    将我们写的Python程序发布成包后,可以使其能够安装使用. 在项目上测试的时候,某些情况下,可以将Python打包,然后上传到测试服务器,安装测试. setuptools是常用的打包工具. 一个简单 ...

随机推荐

  1. vue Baidu Map --- vue百度地图插件

    vue Baidu Map 官网:https://dafrok.github.io/vue-baidu-map/#/zh/start/installation javascript 官网:http:/ ...

  2. 【Git】修改git远程连接ip

  3. HTML元素解释

    <html> 与 </html> 之间的文本描述网页 <body> 与 </body> 之间的文本是可见的页面内容 <head> 与 < ...

  4. react github项目

    https://github.com/bailicangdu/react-pxq 网址;https://github.com/bailicangdu/react-pxq

  5. cygwin 安装 apt-cyg

    apt-cyg apt-cyg is a Cygwin package manager. It includes a command-line installer for Cygwin which c ...

  6. mysql数据库设计三范式

    为了建立冗余较小.结构合理的数据库,设计数据库时必须遵循一定的规则.在关系型数据库中这种规则就称为范式.范式是符合某一种设计要求的总结.要想设计一个结构合理的关系型数据库,必须满足一定的范式. 在实际 ...

  7. java基础语法-char数据类型

    1.java中的char描述了UTF-16中的一个代码单元,因此对于基本的多语言层面可以随意的用char表示 ;//int值只能在0到65535即0000到FFFF 对于其他16个代码级别(两个代码单 ...

  8. socket的原理和实验

    1.socket原理 根据连接启动的方式以及本地套接字要连接的目标,套接字之间的连接过程可以分为三个步骤:服务器监听,客户端请求,连接确认. (1)服务器监听:是服务器端套接字并不定位具体的客户端套接 ...

  9. Net Core API网关Ocelot

    Ocelot在github的地址 https://github.com/TomPallister/Ocelot , 非常给力的是在课程当天完成了.NET Core 2.0的升级,升级过程请看https ...

  10. Java基础总结3

    计算只做加法计算 减法也是加法计算出来的: 1,顺序结构: 按照编写代码的顺序从上而下逐行翻译执行: 特点:每行代码都能被执行到且被执行一次: 2,选择结构看条件: 条件为true(成立)执行代码块: ...