Setuptools是Python Distutils的加强版,使开发者构建和发布Python包更加容易,特别是当包依赖于其他包时。用setuptools构建和发布的包与用Distutils发布的包是类似的。包的使用者无需安装setuptools就可以使用该包。如果用户是从源码包开始构建,并且没有安装过setuptools的话,则只要在你的setup脚本中包含一个bootstrap模块(ez_setup),用户构建时就会自动下载并安装setuptools了。
功能亮点:
python中安装包的方式有很多种:
pip install的东西从哪里来的?
从PyPI (Python Package Index)来的,官网是: https://pypi.python.org/pypi
执行pip install terminaltranslator命令的时候,它就会去从官方网站搜terminaltranslator,搜到了就下载压缩包并解压安装,如果没有搜索到就会报错。
源码包安装就是你在本地编写好了一个模块,自己安装在本地使用,别人(即使是你自己)都不能 pip install xxx 下载你的模块
1.准备工作
# 1.首先创建我们需要的目录结构和文件(自行创建)
# 当前测试的目录是: /tmp/demo
`-- demo
|-- helloapp
| |-- hello.py
| `-- __init__.py
|-- __init__.py
|-- myapp
| |-- __init__.py
| `-- myapp.py
`-- setup.py
from setuptools import setup, find_packages
setup(
name="demo",
version="1.0",
author="zbj",
author_email="22@qq.com",
packages=find_packages(),
)
def hello_func():
print("HelloWorld")
def myapp_func():
print("嘿嘿嘿")
2.源码安装
# 进入setup.py所在的那层目录
cd /tmp/demo
python setup.py check
python setup.py install
3.结果
打包之后多出两个文件夹,分别是demo.egg-info和dist。demo.egg-info是必要的安装信息,
而dist中的压缩包就是安装包,此时默认的egg包,egg包就是zip包,如果需要使用egg包,name将egg后缀改成zip解压即可
4.测试
测试的时候需要注意导包路径和当前所在路径
目前所在路径是: /tmp/demo
直接进入python解释器: python3(我自己安装的python3版本)
5.setuptools更多参数用法
https://setuptools.readthedocs.io/en/latest/setuptools.html
1.创建文件的目录结构
hello/
|-- hello
| |-- hello.py
| `-- __init__.py
|-- LICENSE
|-- README.md
`-- setup.py
2. setup.py
from setuptools import setup, find_packages
setup(
name="hello",
version='1.0',
description="Test Hello",
url="None",
author="zbj",
author_email="22@qq.com",
license="MIT",
packages=find_packages()
)
3. LICENSE
LICENSE代表许可证
Copyright (c) 2018 The Python Packaging Authority
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
4. setuptools 和wheel
首先需要保证你有最新版的setuptools
和wheel
python -m pip install --user --upgrade setuptools wheel
Tip:如果出现问题了可以查看官网的解决方案:https://packaging.python.org/tutorials/installing-packages/
5. 打包模块
# 进入到setup.py同级的目录下
python setup.py sdist bdist_wheel
打包之后多出两个文件夹,分别是hello.egg-info和dist。hello.egg-info是必要的安装信息,而dist中的压缩包就是安装包
dist中包含两个文件:
dist/
|-- hello-1.0-py3-none-any.whl
`-- hello-1.0.tar.gz
6.打包方式介绍
有了上面的 setup.py 文件,我们就可以打出各种安装包,主要分为两类:sdist 和 bdist。
Source distribution
使用 sdist 可以打包成 source distribution,支持的压缩格式有:
Format
Description
Notes
zip
zip file (.zip)
Windows 默认
gztar
gzip’ed tar file (.tar.gz)
Unix 默认
bztar
bzip2’ed tar file (.tar.bz2)
xztar
xz’ed tar file (.tar.xz)
ztar
compressed tar file (.tar.Z)
tar
tar file (.tar)
使用方式为:
$ python setup.py sdist --formats=gztar,zip
现在目录下多出 dist 和 *.egg-info 目录,dist 内保存了我们打好的包,上面命令使用 --formats
指定了打出 .tar.gz
和 .zip
包,如果不指定则如上表根据具体平台默认格式打包。
包的名称为 setup.py
中定义的 name
, version
以及指定的包格式,格式如:firstApp-0.0.1.tar.gz。
Built distribution
使用 bdist 可以打出 built distribution,和源码包相比,由于预先构建好,所以安装更快:
Format
Description
Notes
gztar
gzipped tar file (.tar.gz)
Unix 默认
bztar
bzipped tar file (.tar.bz2)
xztar
xzipped tar file (.tar.xz)
ztar
compressed tar file (.tar.Z)
tar
tar file (.tar)
zip
zip file (.zip)
Windows 默认
rpm
RPM
pkgtool
Solaris pkgtool
sdux
HP-UX swinstall
wininst
self-extracting ZIP file for Windows
msi
Microsoft Installer.
使用上,和 sdist 一样,可以使用 --formats
指定包格式。如:
$ python setup.py bdist --formats=rpm
同时为了简化操作,setuptools 提供了如下命令:
Command
Formats
Notes
bdist_dumb
tar, gztar, bztar, xztar, ztar, zip
Windows 默认 zip, Unix 默认 gztar
bdist_rpm
rpm, srpm
bdist_wininst
wininst
bdist_msi
msi
所以上面打 rpm 包可以使用:
$ python setup.py bdist_rpm
此时前置打包的步骤已经完成,可以开始进行上传。
1.利用twine
将包上传上去,首先安装twine
pip install twine
2.注册 PyPI 账号
登录 https://pypi.python.org/pypi,注册账号
3.上传
# 使用 upload
$ twine upload dist/*
输入 username 和 password 即上传至 PyPI。
[distutils]
index-servers =
pypi
pypitest
[pypi]
username:
password:
[pypitest]
repository: https://test.pypi.org/legacy/
username:
password:
4. 检验
这时候就可以,下载包,然后运行里面方法了
pip install hello
在本地测试的时候可以直接安装打包好的dist下的包:
pip install xxx.tag.gz
测试功能正常后再上传到pypi
提示
5. 更新版本
更新版本也很简单,只需要修改setup.py下的version
然后重新生成档案,上传
python setup.py sdist bdist_wheel
twine upload dist/hello-0.0.2*
6.更新本地moudle版本
pip install --upgrade hello
或者是先卸载,再安装
pip uninstall hello
pip install hello
上面使用setuptools时只是简单的用一个配置文件setup.py就完成了打包信息填写。在真实的开发环境中,往往是多个文件配合。以openstack的打包为例。openstack中引入了Pbr的管理工具。
pbr是setuptools的辅助工具,最初为openstack开发,基于d2to1。Pbr会读取和过滤setup.cfg中的内容,然后将解析后的数据提供给setup.py作为参数。
setup.cfg提供setup.py的默认参数,同时易于修改。Setup.py先解析setup.cfg文件,然后执行相关命令。包括以下功能:
1、从git中获取Version,AUTHORS和ChangeLog信息
2、SphinxAutodoc。pbr会扫描project,找到所有模块,生成stubfiles
3、Requirements。读取requirements.txt文件,生成setup函数需要依赖包
4、long_description。从README.rst、README.txt或者READMEfile中生成long_description参数
Pbr的文件很简单,如下。配置之后会自动寻找目录下的setup.cfg文件,解析文件参数给setup.py使用。
https://docs.openstack.org/pbr/latest/user/using.html
setup.py
from setuptools import setup
setuptools.setup(
setup_requires=['pbr'],
pbr=True)
setup.cfg
[metadata]
name = my_package
version = attr: src.VERSION
description = My package description
long_description = file: README.rst, CHANGELOG.rst, LICENSE.rst
keywords = one, two
license = BSD 3-Clause License
classifiers =
Framework :: Django
License :: OSI Approved :: BSD License
Programming Language :: Python :: 3
Programming Language :: Python :: 3.5
[files]
packages =
project_name
data_files =
etc/pbr = etc/pbr/*
[global]
setup-hooks =
pbr.hooks.setup_hook
[entry_points]
console_scripts =
project_name = project.cmd.mycmd:main
[options]
zip_safe = False
include_package_data = True
packages = find:
scripts =
bin/first.py
bin/second.py
install_requires =
requests
importlib; python_version == "2.7"
[options.package_data]
* = *.txt, *.rst
hello = *.msg
[options.extras_require]
pdf = ReportLab>=1.2; RXP
rest = docutils>=0.3; pack ==1.1, ==1.3
[options.packages.find]
exclude =
src.subpackage1
src.subpackage2
[options.data_files]
/etc/my_package =
site.d/00_default.conf
host.d/00_default.conf
data = data/img/logo.png, data/svg/icon.svg
手机扫一扫
移动阅读更方便
你可能感兴趣的文章