python打包分发工具setuptools使用记录
2018-11-27 08:32:19来源:博客园 阅读 ()
关于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!
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
下一篇:python学习之旅(十六)
- python3基础之“术语表(2)” 2019-08-13
- python3 之 字符串编码小结(Unicode、utf-8、gbk、gb2312等 2019-08-13
- Python3安装impala 2019-08-13
- 小白如何入门 Python 爬虫? 2019-08-13
- python_字符串方法 2019-08-13
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash