关于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. xtrabackup工具备份与恢复

    1.xtrabackup简介 Xtrabackup是一个对InnoDB做数据备份的工具,支持在线热备份(备份时不影响数据读写),是商业备份工具InnoDB Hotbackup的一个很好的替代品.它能对 ...

  2. 【Mac】-NO.133.Mac.1 -【重置忘记macos root密码】

    Style:Mac Series:Java Since:2018-09-10 End:2018-09-10 Total Hours:1 Degree Of Diffculty:5 Degree Of ...

  3. 2019年IntelliJ IDEA 最新注册码,亲测可用(截止到2020年3月11日)

    2019年IntelliJ IDEA 最新注册码(截止到2020年3月11日) 操作步骤: 第一步:  修改 hosts 文件 ~~~ 在hosts文件中,添加以下映射关系: 0.0.0.0 acco ...

  4. python 计算器

    import redef main(): #""代表的是空,split()方法已空格或者\t,\n进行切割,join方法是列表,元组,字典变为字符串 a = "" ...

  5. Percona-Toolkit 之 pt-table-checksum 总结

    pt-table-checksum - Verify MySQL replication integrity. pt-table-checksum performs an online replica ...

  6. GAITC 2019全球人工智能技术大会(南京)

    2019年5月25日至26日,由中国人工智能学会主办,以“交叉.融合.相生.共赢”为主题的2019GAITC将在南京全新亮相. 2019 全球人工智能技术大会(2019 GAITC)以“前端引领.深度 ...

  7. OO第一次博客

    过去的三周里我们完成了表达式求导的程序设计与构造.表达式求导程序,大致思路是实现一个表达式类,支持表达式的输入.求导运算和输出功能.可能的话,还可以增加表达式的化简方法,从而得到更高质量的输出结果.总 ...

  8. win 下 python ImportError: No module named requests

    第一次弄爬虫,报库找不到,网上找了半天,一般都让都让改成绝对路径...那不是饮鸩止渴嘛. 然后 在无意中发现,不需要控制台输入pip命令,因为不是在Linux下啊,,win下直接在pycharm里添加 ...

  9. Python练习:含参数的脚本示例

    首先准备一个example.csv文件,如下: 编写脚本test.py ,实现传入参数,读取example.csv文件,并将其保存为另一个文件, #  含参数的脚本,读取一个文件,并另保存一个文件im ...

  10. linux安装sz && rz功能

    [1]编译安装 root 账号登陆后,依次执行以下命令: cd /tmp wget http://www.ohse.de/uwe/releases/lrzsz-0.12.20.tar.gz . ./c ...