Python的打包工具(setup.py)实战篇
<p><strong> <span style="font-size: 18pt">Python的打包工具(setup.py)实战篇</span></strong></p><p><span style="font-size: 18pt"><strong> 作者:尹正杰 </strong></span></p>
<p><span style="font-size: 18pt"><strong>版权声明:原创作品,谢绝转载!否则将追究法律责任。</strong></span></p>
<p> </p>
<p> </p>
<p><strong><span style="font-size: 14pt"> 当你写好一个Python项目时,想要开源或者给别的小伙伴使用,这个时候就可以考虑使用打包工具来做这个事情,Python内置的setup.py工具就自带了这个功能,本片博客将带你来打包一个自己写的python项目。</span></strong></p>
<p><strong><span style="font-size: 14pt"> 学会python打包后,可以把自己写好的模块发布到公共的PyPI(https://pypi.org/)上,也可以搭建PyPI私服,供企业内部使用。</span></strong></p>
<p><strong><span style="font-size: 14pt"> 目前PyPI里面的模块没用太好的审核机制,不保证安全,因此要慎重使用。据说,数据分析领域基于python的开源模块在不同的操作系统算出的结果竟然不同,这意味着所有基于该模块的计算发表的论文结果都是不准确的!</span></strong></p>
<p> </p>
<p> </p>
<p> </p>
<p><span style="font-size: 14pt; color: rgba(255, 0, 255, 1)"><strong>一.查找setup帮助文档</strong></span></p>
<p><strong><span style="font-size: 18px">1>.找到setup的帮助文档,如下图所示</span></strong></p>
<p><img src="https://img2020.cnblogs.com/blog/795254/202012/795254-20201212143920563-925893124.png"></p>
<p><strong><span style="font-size: 18px">2>.如下图所示,点击"Distttributing Python Modules(Legacy version)"</span></strong></p>
<p><img src="https://img2020.cnblogs.com/blog/795254/202012/795254-20201212144109949-983535432.png"></p>
<p><strong><span style="font-size: 18px">3>.如下图所示,点击"Writing the Setup Script"</span></strong></p>
<p><img src="https://img2020.cnblogs.com/blog/795254/202012/795254-20201212144237337-455296981.png"></p>
<p><strong><span style="font-size: 18px">4>.阅读官网文档</span></strong></p>
<p><img src="https://img2020.cnblogs.com/blog/795254/202012/795254-20201212144450667-1735894552.png"></p>
<p><strong><span style="font-size: 18px">5>.在自己的项目根目录下创建setup.py文件,修改内容如下所示</span></strong></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> !/usr/bin/env python</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> -*- coding:utf-8 -*-</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> @author :yinzhengjie</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> blog:https://www.cnblogs.com/yinzhengjie</span>
<span style="color: rgba(0, 0, 255, 1)">from</span> distutils.core <span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> setup
setup(
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 指定项目名称,我们在后期打包时,这就是打包的包名称,当然打包时的名称可能还会包含下面的版本号哟~</span>
name=<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">devops</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 指定版本号</span>
version=<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">0.1.1</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 这是对当前项目的一个描述</span>
description=<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">Python automatic operation and maintenance platform</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 作者是谁,指的是此项目开发的人,这里就写你自己的名字即可</span>
author=<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">yinzhengjie</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 作者的邮箱</span>
author_email=<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">y1053419035@qq.com</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 写上项目的地址,比如你开源的地址开源写博客地址,也开源写GitHub地址,自定义的官网地址等等。</span>
url=<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">https://www.cnblogs.com/yinzhengjie/p/14124623.html</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 指定包名,即你需要打包的包名称,要实际在你本地存在哟,它会将指定包名下的所有"*.py"文件进行打包哟,但不会递归去拷贝所有的子包内容。</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 综上所述,我们如果想要把一个包的所有"*.py"文件进行打包,应该在packages列表写下所有包的层级关系哟~这样就开源将指定包路径的所有".py"文件进行打包!</span>
packages=[<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">devops</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">devops.dev</span><span style="color: rgba(128, 0, 0, 1)">"</span>, <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">devops.ops</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">],
)</span></pre>
</div>
<p><img src="https://img2020.cnblogs.com/blog/795254/202012/795254-20201212155426585-147454155.png"></p>
<p> </p>
<p><span style="font-size: 14pt; color: rgba(255, 0, 255, 1)"><strong>二.使用setup.py工具对自定义package进行打包操作</strong></span></p>
<p><strong><span style="font-size: 18px">1>.查看setup.py工具的帮助信息</span></strong></p>
<div class="cnblogs_code">
<pre>python setup.py --help-commands</pre>
</div>
<p><img src="https://img2020.cnblogs.com/blog/795254/202012/795254-20201212153452924-1554164322.png"></p>
<p><strong><span style="font-size: 18px">2>.编译python的包(本质上是新建了一个build目录,而后将指定的packages列表包下的所有".py"文件拷贝过去)</span></strong></p>
<div class="cnblogs_code">
<pre>python setup.py build</pre>
</div>
<p><img src="https://img2020.cnblogs.com/blog/795254/202012/795254-20201212155811217-1589819477.png"></p>
<p><strong><span style="font-size: 18px">3>.将源文件进行打包操作</span></strong></p>
<div class="cnblogs_code">
<pre>python setup.py sdist</pre>
</div>
<p><img src="https://img2020.cnblogs.com/blog/795254/202012/795254-20201212160535965-384477595.png"></p>
<p><strong><span style="font-size: 18px">4>.基于我们刚刚打包的文件进行安装</span></strong></p>
<div class="cnblogs_code">
<pre>pip install test/dist/devops-0.1.1.tar.gz</pre>
</div>
<p><img src="https://img2020.cnblogs.com/blog/795254/202012/795254-20201212163320708-1186256027.png"></p>
<p><strong><span style="font-size: 18px">5>.卸载咱们刚刚安装的包</span></strong></p>
<div class="cnblogs_code">
<pre>pip uninstall devops</pre>
</div>
<p><img src="https://img2020.cnblogs.com/blog/795254/202012/795254-20201212163726202-120185290.png"></p>
<p> </p>
<p><span style="font-size: 14pt; color: rgba(255, 0, 255, 1)"><strong>三.bdist命令</strong></span></p>
<div class="cnblogs_code">
<pre> bdist命令是一个二进制分发包,或称作安装程序。该命令可以生成模板操作系统的安装程序。</pre>
</div>
<p><span style="font-size: 18px"><strong>1>.制作windows下的安装包</strong></span></p>
<div class="cnblogs_code">
<pre>python setup.py bdist_wininst<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 创建"*.exe"的文件</span>
python setup.py bdist_msi<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 创建"*.msi"的文件</span>
python setup.py bdist --format=msi<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 同样是创建"*.msi"的文件</span></pre>
</div>
<p><img src="https://img2020.cnblogs.com/blog/795254/202012/795254-20201212164849907-416466816.png"></p>
<p><span style="font-size: 18px"><strong>2>.制作rpm包</strong></span></p>
<div class="cnblogs_code">
<pre>python setup.py bdist_rpm<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 创建"*.rpm"的文件,该命令需要在Linux操作系统上执行!</span>
python setup.py bdist --format=rpm<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 同上</span></pre>
</div>
<p><span style="font-size: 18px"><strong>3>.制作压缩文件</strong></span></p>
<div class="cnblogs_code">
<pre>python setup.py bdist --format=zip<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 创建"*.zip"压缩文件</span>
python setup.py bdist --format=gztar<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 创建"*.tar.gz"文件</span></pre>
</div>
<p><img src="https://img2020.cnblogs.com/blog/795254/202012/795254-20201212165541480-1802892968.png"></p>
<p> </p>
<p><span style="color: rgba(255, 0, 255, 1)"><strong><span style="font-size: 14pt">四.将python打包成<strong>egg包</strong>或者<strong>whl包(本质上是一个zip文件)</strong></span></strong></span></p>
<p><span style="color: rgba(0, 0, 0, 1)"><strong><span style="font-size: 18px">1>.安装wheel模块</span></strong></span></p>
<div class="cnblogs_code">
<pre>pip install wheel</pre>
</div>
<p><img src="https://img2020.cnblogs.com/blog/795254/202012/795254-20201212173133869-164253600.png"></p>
<p><span style="color: rgba(0, 0, 0, 1)"><strong><span style="font-size: 18px">2>.修改"setup.py"文件</span></strong></span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> !/usr/bin/env python</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> -*- coding:utf-8 -*-</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> @author :yinzhengjie</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> blog:https://www.cnblogs.com/yinzhengjie</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> from distutils.core import setup</span>
<span style="color: rgba(0, 0, 255, 1)">from</span> setuptools <span style="color: rgba(0, 0, 255, 1)">import</span> setup<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 注意哈,setuptools是基于distutils进行封装的,但打wheel包时要从setuptools包导入setup模块哟~</span>
<span style="color: rgba(0, 0, 0, 1)">
setup(
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 指定项目名称,我们在后期打包时,这就是打包的包名称,当然打包时的名称可能还会包含下面的版本号哟~</span>
name=<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">devops</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 指定版本号</span>
version=<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">0.1.1</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 这是对当前项目的一个描述</span>
description=<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">Python automatic operation and maintenance platform</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 作者是谁,指的是此项目开发的人,这里就写你自己的名字即可</span>
author=<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">yinzhengjie</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 作者的邮箱</span>
author_email=<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">y1053419035@qq.com</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 写上项目的地址,比如你开源的地址开源写博客地址,也开源写GitHub地址,自定义的官网地址等等。</span>
url=<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">https://www.cnblogs.com/yinzhengjie/p/14124623.html</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 指定包名,即你需要打包的包名称,要实际在你本地存在哟,它会将指定包名下的所有"*.py"文件进行打包哟,但不会递归去拷贝所有的子包内容。</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 综上所述,我们如果想要把一个包的所有"*.py"文件进行打包,应该在packages列表写下所有包的层级关系哟~这样就开源将指定包路径的所有".py"文件进行打包!</span>
packages=[<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">devops</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">devops.dev</span><span style="color: rgba(128, 0, 0, 1)">"</span>, <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">devops.ops</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">],
)</span></pre>
</div>
<p><span style="color: rgba(0, 0, 0, 1)"><strong><span style="font-size: 18px">3>.打包whl和egg格式,如下图所示</span></strong></span></p>
<div class="cnblogs_code">
<pre>python setup.py bdist_egg<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 打"*.egg"的包</span>
python setup.py bdist_wheel<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 打"*.whl"的包</span></pre>
</div>
<p><img src="https://img2020.cnblogs.com/blog/795254/202012/795254-20201212174125515-1521145450.png"></p>
<p> </p>
</div>
<div id="MySignature" role="contentinfo">
<p>本文来自博客园,作者:尹正杰,转载请注明原文链接:https://www.cnblogs.com/yinzhengjie/p/14124623.html,个人微信: "JasonYin2020"(添加时请备注来源及意图备注,有偿付费) </p>
<p>当你的才华还撑不起你的野心的时候,你就应该静下心来学习。当你的能力还驾驭不了你的目标的时候,你就应该沉下心来历练。问问自己,想要怎样的人生。</p><br><br>
来源:https://www.cnblogs.com/yinzhengjie/p/14124623.html
頁:
[1]