使用Setuptools构建和分发软件包

参考官方文档翻译

开发人员指南

安装setuptools

安装最新版本的setuptools:

python3 -m pip install --upgrade setuptools

基本使用

引入setuptools基本使用

from setuptools import setup, find_packages
setup(
name="HelloWorld",
version="0.1",
packages=find_packages(),
)

要生成源代码分发,只需调用:

setup.py sdist

随着项目的增大将会包括一些依赖项,以及一些数据文件和脚本:

from setuptools import setup, find_packages
setup(
name="HelloWorld",
version="0.1",
packages=find_packages(),
scripts=["say_hello.py"], # Project uses reStructuredText, so ensure that the docutils get
# installed or upgraded on the target machine
install_requires=["docutils>=0.3"], package_data={
# If any package contains *.txt or *.rst files, include them:
"": ["*.txt", "*.rst"],
# And include any *.msg files found in the "hello" package, too:
"hello": ["*.msg"],
}, # metadata to display on PyPI
author="Me",
author_email="me@example.com",
description="This is an Example Package",
keywords="hello world example examples",
url="http://example.com/HelloWorld/", # project home page, if any
project_urls={
"Bug Tracker": "https://bugs.example.com/HelloWorld/",
"Documentation": "https://docs.example.com/HelloWorld/",
"Source Code": "https://code.example.com/HelloWorld/",
},
classifiers=[
"License :: OSI Approved :: Python Software Foundation License"
] # could also include long_description, download_url, etc.
)

下面我们将解释大多数这些setup() 参数的作用(元数据除外),以及在您自己的项目中使用它们的各种方式

指定项目的版本

官方的划分比较多,简洁的说一下:

版本:发行号+标签(交替出现)

发行号:2.1.0 (简单理解格式)

标签:类似alpha,beta, a,c,dev等,例如:一个不稳定的预发行版本,建议:2.1.0.a9,如果需要加入日期建议使用-,例如:2.1.0.9-20190604

可以使用该pkg_resources.parse_version()功能比较不同的版本号:

>>> from pkg_resources import parse_version
>>> parse_version("1.9.a.dev") == parse_version("1.9a0dev")
True
>>> parse_version("2.1-rc2") < parse_version("2.1")
True
>>> parse_version("0.6a9dev-r41475") < parse_version("0.6a9")
True

新增和更改的setup()关键字

include_package_data:如果设置为True,setuptools则表示将在MANIFEST.in文件指定的包目录中自动包含找到的所有数据文件。

exclude_package_data:将包名称映射到应该从包目录中排除的全局模式列表的字典

package_data:字典将包名称映射到全局模式列表。

zip_safe:一个布尔值(True或False)标志,指定是否可以安全地安装该项目并从zip文件运行该项目。如果未提供此参数,则bdist_egg每次构建鸡蛋时,该命令将必须分析项目的所有内容以查找可能的问题。

install_requires:一个字符串或字符串列表,指定在安装此版本时还需要安装其他发行版。

entry_points:字典将入口点组名称映射到定义入口点的字符串或字符串列表。

extras_require:字典将名称“ extras”(项目的可选功能)映射到字符串或字符串列表的字典,用于指定必须安装哪些其他发行版来支持这些功能。

python_requires:python版本设置。

版本号大于等于3.3,但是不能超过4
python_requires='~=3.3',
支持2.6 2.7以及所有以3.3开头的Python 3版本
python_requires='>=2.6, !=3.0.*, !=3.1.*, !=3.2.*, <4',

setup_requires:安装脚本执行时需要依赖的分发包,主要用于构建过程,注意,这里列出的包不会自动安装,如果需要,同时要在install_requires中指定。

dependency_links:满足依赖性时要搜索的URL的字符串列表

namespace_packages:命名项目的“命名空间包”的字符串列表

包括数据文件

对包含的文件进行更细粒度的控制(例如,如果您的软件包目录中有文档文件,并希望将其排除在安装范围之外),则还可以使用package_data关键字

setup.py
src/
mypkg/
__init__.py
mypkg.txt
data/
somefile.dat
otherdata.dat
from setuptools import setup, find_packages
setup(
...
packages=find_packages("src"), # include all packages under src
package_dir={"": "src"}, # tell distutils packages are under src package_data={
# If any package contains *.txt files, include them:
"": ["*.txt"],
# And include any *.dat files found in the "data" subdirectory
# of the "mypkg" package, also:
"mypkg": ["data/*.dat"],
}
)

如果想去掉readm.md等文件:

from setuptools import setup, find_packages
setup(
...
packages=find_packages("src"), # include all packages under src
package_dir={"": "src"}, # tell distutils packages are under src include_package_data=True, # include everything in source control # ...but exclude README.txt from all packages
exclude_package_data={"": ["README.txt"]},
)

参考示例

#!/usr/bin/env python
# -*- coding: UTF-8 -*- from setuptools import setup, find_packages """
setuptools使用文档: https://setuptools.readthedocs.io/en/latest/setuptools.html 生成源码发布包, 在灵犀工程目录(LingXi/)下执行: python3 setup.py sdist
""" setup(
name="LingXi",
version="1.5.0", # 发布前注意将此处改为本次发布的版本号
packages=find_packages(), # Project uses reStructuredText, so ensure that the docutils get
# installed or upgraded on the target machine
# install_requires=['docutils>=0.3'], package_data={
# If any package contains *.txt or *.rst files, include them:
'': ['*.txt', '*.otf', '*.ttc', '*.sh', '*.css', '*.js', '*.ttf', '*.eot', '*.svg', '*.woff'],
'perception': ['templates/*',
'blocks/econet/*.yaml',
'compiled/Makefile',
'compiled/center_net/_cpools/src/*.cpp', 'compiled/center_net/_nms/*.c',
'compiled/center_net/_nms//pointer/resnet50/*',
'modules/meter_recognition_v2/cfgs/*',
'modules/meter_recognition_v2/cfgs/resnet50/*',
],
'services': ['*.sh',
'lingxi/static/js/*.js', 'lingxi/static/audio/*.wav',
'lingxi/static/docs/*.pdf', 'lingxi/static/imgs/*.jpg',
'lingxi/static/*.html', 'lingxi/templates/*.html',
'lingxi/static/*.css', 'lingxi/static/*.ico']
}, # metadata to display on PyPI
author="",
author_email="",
maintainer="",
maintainer_email="{dzp, gy, xkk}@kilox.cn",
description="分析系统",
keywords="",
url="",
license="GPLv3",
platforms="UNIX",
long_description="README.md, CHANGELOG.md, LICENSE",
install_requires=['opencv-python', 'numpy', 'Pillow', 'six', 'torch', 'pytz', 'waitress', 'wtforms', 'flask',
'torchvision', 'termcolor', 'scipy', 'matplotlib', 'imageio', 'requests', 'soundfile', 'librosa',
'face_recognition', 'scikit-image', 'imutils', 'tensorflow', 'mmcv', 'pycocotools', 'tabulate',
'psutil', 'py-cpuinfo', 'PyYAML', 'fvcore']
)

用Setuptools构建和分发程序包的更多相关文章

  1. 全面学习 Python 包:包的构建与分发

    首发于公众号:Python编程时光 1. 为什么需要对项目分发打包? 平常我们习惯了使用 pip 来安装一些第三方模块,这个安装过程之所以简单,是因为模块开发者为我们默默地为我们做了所有繁杂的工作,而 ...

  2. 【Azure DevOps系列】Azure DevOps构建并发布Nuget程序包

    在Azure DevOps中,管道可以用来构建解决方案,O(∩_∩)O哈哈~快万能了,本章主要介绍如何创建Nuget包并且将其发布到Nuget服务器的过程. 前面我创建了一个非常简单的类库,这边我不做 ...

  3. Spring-boot构建多模块依赖工程时,maven打包异常:程序包xxx不存在

    在qizhi项目改版的时候, 所有代码都迁移好了, 但是compile的时候报程序包*****不存在, 具体到某一个类就是: 找不到符号. 下面这篇文章是正解 http://hbxflihua.ite ...

  4. 想拥有自己的Python程序包,你只需15步

    来源商业新知网,原标题:15步,你就能拥有自己的Python程序包 全文共 3192 字,预计学习时长 6 分钟 每个软件开发员和数据科学家都难免要做程序包.本文推荐一篇 Python开源程序包的制作 ...

  5. 使用Beetle简单构建聊天室程序

    之前已经讲解了Beetle简单地构建网络通讯程序,那程序紧紧是讲述了如何发送和接收数据:这一章将更深入的使用Beetle的功能,主要包括消息制定,协议分析包括消息接管处理等常用的功能.为了更好的描述所 ...

  6. [译]基于Vue.js的10个最佳UI框架,用于构建移动应用程序

    原文查看10 Best Vue.js based UI Frameworks for Building Mobile Apps 如果您期待使用Vue.js构建移动应用程序,那么您可以选择许多可用的UI ...

  7. Linux程序包管理之rpm

    rpm简介 rpm( Red Hat Package Manager )是一个开放的软件包管理系统.它工作于Red Hat Linux及其他Linux系统,成为Linux中公认的软件包管理标准. rp ...

  8. Linux程序包管理之yum及源代码安装

    第十六章.Linux程序包管理之yum及源代码安装 目录 yum介绍 yum配置文件 yum的repo配置文件中可用的变量 yum命令的使用 使用光盘作为本地yum仓库 如何创建yum仓库 编译安装的 ...

  9. Linux程序包管理.md

    rpm 简介 RPM包管理员(简称RPM,全称为The RPM Package Manager)是在Linux下广泛使用的软件包管理器.RPM此名词可能是指.rpm的文件格式的软件包,也可能是指其本身 ...

随机推荐

  1. Margin of Error|sample size and E

    8.3 Margin of Error 由该公式可知: To improve the precision of the estimate, we need to decrease the margin ...

  2. Eclipse创建java web工程

    Eclipse创建java web工程 eclipse版本:eclipse-jee-4.5-win32-x64 tomcat版本:apache-tomcat-7.0.63-windows-x64 jd ...

  3. 1040 有几个PAT (25 分)

    题目:1040 有几个PAT (25 分) 思路: 是个规律题,只要找到规律就有思路,那代码基本就有了,就是怎么实现比较好和是否简洁的问题. 很明显:A是分水岭,A前面有多少个P和A后面有多少个T知道 ...

  4. E丢丢App重设计总结

    E丢丢学习App是华夏大地教育可以有限公司旗下的一款产品,专为提升学历者打造,它整合了线上+跟踪的 (E平台)功能,方便工作人员随时随地管理账号.跟进学员:同时还可以随时了解教育行业的新闻资讯.一对一 ...

  5. 手机安装fiddler证书

    如果电脑浏览器和手机抓包有证书问题,那就把电脑的证书都删除,然后在fiddler里重置,手机上删除不了单个证书,可以重新下载一个证书安装 如果电脑抓包正常,手机抓包不正常,那就手机重新下载证书安装 手 ...

  6. Python-多任务复制文件夹

    import multiprocessing import os import time def copy_file(queue, file_name, old_folder_name, new_fo ...

  7. Docker For Mac 下安装 Rancher

    https://www.jianshu.com/p/5fb3e1a998d6 Docker For Mac 下安装 Rancher 原文:如何在 OS X 上安装 Rancher Rancher 是 ...

  8. Windows XP系列全下载(均为MSDN原版)

    正版windows xp sp3 下载大全(附:正版密钥) 微软MSDN Windows XP Professional下载 Windows XP Professional 简体中文 (最原始版本,无 ...

  9. SpringMVC之@SessionAttribute和@ModelAttribute

    1.Controller package com.tz.controller; import java.util.Map; import org.springframework.stereotype. ...

  10. connect() failed (111: Connection refused) while connecting to upstream报错处理

    新lnmp环境调试项目时,nginx报错如下: 解决: 发现php-fpm.conf是以套接字方式通信,而nginx是以端口方式通信,见下图: 将nginx.conf修改为如下,重新reload即可