分发工具setuptools
一般 Python 安装会自带 setuptools,如果没有可以使用 pip 安装:(注意:python2,python3都安装了,若要使用python3,需要使用pip3命令,否则默认安装在python2目录下)
$ pip install setuptools
组织项目目录:
build:编译自动生成的,暂不考虑
dist:编译自动生成的,暂不考虑
docs:这个文件夹主要是存放的当前目录的文档信息(快速了解包、模块)
pyproject-utils:这里是主要的代码位置,存放框架中的各种信息:
./base: 代码基本内容
./config: 配置信息,模块的一些默认信息
./exception: 代码中可能出现的异常类, 主要在这个文件夹中。
./script: 这里一些常用脚本,命令行操作的脚本等
./test: 测试的文件夹,主要是对于模块中的重要代码进行测试
./utils: 一些常用的工具,代码中一些通用的、简单的、不具体属于特定类的函数方法等
./main.py:程序的主要入口。
script:外部脚本,与内部脚本只是空间不同
LICENSE.txt:许可证信息,一般都要填上的
README.md:要对读者说的话。
requirements.txt:程序中的所有依赖(可以用pip freese > requirement.txt生成
setup.cfg:打包的设置(这里设置了,就不需要在setup.py中设置了)
setup.py:打包的设置
编码
将程序中主要代码完成
编写setup打包设置
1. 新建一个setup.py编译文件,内容如下:
# !/usr/local/bin/python
# coding=utf-8
# __author__ = 'admin'
from setuptools import setup, find_packages
with open('README.md', 'r') as rd:
long_description = rd.read()
setup(
name='pyproject-utils', # project Name
version='0.1.0', # project version number
packages=find_packages(),
# auto find python packages in the project
description = 'test framework'
url = "http://gitserver-project.git",
author='authorname',
author_email='email.authoer@xx.cn',
license='MIT',
long_description = long_description,
keywords = 'api test python language frameworks',
# include_package_data=True,
# zip_safe = True,
# scripts = ['main.py'],
python_requires='>=3' # depand on python version
# use pip to install these python dependancy libraries.
install_requires = [
'docutils>=0.3',
'Appium-Python-Client>=0.44',
'PyMySQL>=0.9.3',
'PyYAML>=5.1.1',
'configparser>=3.7.4',
'et-xmlfile>=1.0.1',
'lxml>=4.3.4',
'openpyxl>=2.6.2',
'pandas>=0.24.2',
'pip>=19.1.1',
'pymssql>=2.1.4',
'requests>=2.22.0',
'selenuim>=3.141.0',
'setuptools>=41.0.1',
'urllib3>=1.25.3',
'wheel>=0.33.4',
'pywin32>=224',
'win32core==221.36',
'win32gui==221.6',
'xlrd==1.2.0',
'xlutils==2.0.0',
'xlwt==1.3.0' ],
package_data={
# If any package contains *.yml ..files, include them:
'': ['*.yml', '*.ini','*.xlsx', '*.xls', '*.md', '*.rst', '*.txt'],
# And include any *.xml files found in the 'objMap' package, too:
'objMap': ['*.xml'],
},
# if the data file is out of the project directory, use data_files or MANIFEST.in
# if to use wheel, here use data_files
# if to use source package, here use MANIFEST.in
# data_files=[('mydata', ['data/conf.yml'])],
# add the 'data/conf.yml' to 'mydata' dir
# project classify
classifiers=[
# How mature is this project? Common values are
# 3 - Alpha
# 4 - Beta
# 5 - Production/Stable
'Development Status :: 3 - Alpha',
# Indicate who your project is intended for
'Intended Audience :: Developers',
'Topic :: Software Development :: Build Tools',
# Pick your license as you wish (should match "license" above)
'License :: OSI Approved :: MIT License',
# Specify the Python versions you support here. In particular, ensure
# that you indicate whether you support Python 2, Python 3 or both.
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.6',
'Operating System :: OS Independent',
],
project_urls = {
"project_git_address" : "http://gitserver-project.git",
}
)
命令检查setup.py:
>>python setup.py check
可以检查setup.py是不是正确,如果只输出running check,那么就ok了
运行setup.py打包:
>>python setup.py sdist #打包后的格式为tar.gz/zip
运行结果:
当前目录下新增一个dist目录,里面会有一个同name值相同的文件包。Windows下时zip包,linux下是tar.gz包。
安装并测试:
解压刚打包好的文件,运行如下命令进行安装:
python setup.py install
卸载:
python setup.py uninstall
setup.py打包命令各参数详解:
>>python setup.py --help-commands
--python setup.py build # 仅编译不安装
--python setup.py install #安装到python安装目录的lib下
--python setup.py sdist #生成压缩包(zip/tar.gz)
--python setup.py bdist_wininst #生成NT平台安装包(.exe)
--python setup.py bdist_rpm #生成rpm包
或者直接"bdist 包格式",格式如下:
#python setup.py bdist --help-formats
--formats=rpm RPM distribution
--formats=gztar gzip'ed tar file
--formats=bztar bzip2'ed tar file
--formats=ztar compressed tar file
--formats=tar tar file
--formats=wininst Windows executable installer
--formats=zip ZIP file
如:
python setup.py bdist --formats=zip 等价于 python setup.py sdist
setup函数的参数:
setup函数各参数详解:
>>python setup.py --help
name
工程名字,名字中可以包含的字符见 `PEP426 <http://legacy.python.org/dev/peps/pep-0426/#name>`_
version
工程版本,格式见 `PEP440 <https://pypa.io/en/latest/peps/#pep440s>`_:
1.2.0.dev1 # Development release
1.2.0a1 # Alpha Release
1.2.0b1 # Beta Release
1.2.0rc1 # RC Release
1.2.0 # Final Release
1.2.0.post1 # Post Release
description
工程描述
url
工程地址
author
作者信息
author_email
程序作者的邮箱地址
maintainer
维护者
maintainer_email
维护者的邮箱地址
license
许可证信息
classifiers
一组你的工程相关的信息,PyPI会利用这些信息来给你的工程分类,见 https://pypi.python.org/pypi?%3Aaction=list_classifiers
keywords
一组关键字来描述你的工程
packages
你的工程中包含的package,需要列出所需的所有package和subpackage,disutils不会自动寻找subpackage
platforms
程序适用的软件平台列表
py_modules
需要打包的python文件列表
download_url
程序下载地址
scripts
安装时需要执行的脚本列表
install_requires
依赖列表,pip安装时候会根据这个列表来自动安装所需依赖库,比如sphinx的依赖列表::
install_requires = [
'six>=1.4',
'Jinja2>=2.3',
'Pygments>=1.2',
'docutils>=0.10',
'snowballstemmer>=1.1',
'babel',
]
package_data
package中的数据列表,生成库的时候这个列表中的数据都会被加进MANIFEST文件
安装库的时候,这个列表中的数据会被安装
cmdclass
添加自定义命令
data_files
和package_data的区别在于,data_files列出的数据不在package中
include_package_data
设成True,则自动添加你的工程目录中的所有的文件,如果没有额外的指明,只添加全部的文件
exclude_package_data
指明了需要排除的文件
package_data
指明了需要添加的文件
zip_safe
指明你的工程是否能够以压缩的格式安装
install_requires
依赖