关于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. 利用dladdr来获得so自身的路径

    #include <dlfcn.h> //定义该函数为了dladdr获取符号信息 void fun1() { } Dl_info info; //dladdr获取某个地址的符号信息 int ...

  2. extjs 跨域 ajax.request

    https://www.cnblogs.com/yuzhongwusan/p/3677955.html https://stackoverflow.com/questions/25727306/req ...

  3. mac 安装robot framework报错:No matching distribution found for Pywin32

    运行  pip install robotframework-ride  ,出现以下错误 解决办法:修改Python的运行方式,需要32位的python运行 defaults write com.ap ...

  4. ionic3 小记录

    cordova platform add ios@latest 安装最新ios ionic cordova build ios -- --buildFlag="-UseModernBuild ...

  5. spring boot 热部署,省去频繁编译的步骤

    一.热启动: 每自修改后, 程序自动启动Spring Application上下文. Pom中直接添加依赖即可: <dependency>            <groupId&g ...

  6. Vue学习之路---No.3(分享心得,欢迎批评指正)

    同样的,我们先来回顾一下昨天学习的内容: 1.利用v-once来组织双向绑定 2.filter{}过滤器的使用(详情请看上一章) 3.computed(计算属性),利用computed属性实现filt ...

  7. Linux环境上部署Flask

    [该文章只涉及个人部署的简单流程,读者可通过其它途径了解详细部署流程] 依个人部署项目可预先安装好需要的环境,这里已提前安装好LNMP环境 1.安装Python环境 安装virtualenv环境 配置 ...

  8. spring boot 整合js css 静态文件

    一,添加配置 spring: application: name: interview-server resources: static-locations: file:config/statics ...

  9. ThinkAdmin for PHP后台管理系统

    ThinkAdmin for PHP后台管理系统 ThinkAdmin 是一个基于 Thinkphp 5.1.x 开发的后台管理系统,集成后台系统常用功能.基于 ThinkPHP 5.1 基础开发平台 ...

  10. java面试经常问到的计算机网络问题

    GET 和 POST 的区别 GET请注意,查询字符串(名称/值对)是在 GET 请求的 URL 中发送的:/test/demo_form.asp?name1=value1&name2=val ...