欧菲 發表於 2020-6-29 18:33:00

PyInstaller打包Python项目详解

<h2 id="pyinstaller打包python详解"><strong>PyInstaller打包Python详解</strong></h2>
<ul>
<li>官网参考</li>
</ul>
<h4 id="一pyinstaller简介">一、PyInstaller简介</h4>
<blockquote>
<p>1.PyInstaller可以将Python项目在不同平台上打包为可执行文件.</p>
<p>2.PyInstaller打包的流程:读取编写好的Python项目--&gt;分析其中条用的模块和库,并收集其文件副本(包括Python的解释器)--&gt;将副本和Python项目文件(放在一个文件夹//封装在一个可执行文件)中.</p>
</blockquote>
<h4 id="二pyinstaller的参数列表">二、pyinstaller的参数列表</h4>
<table>
<thead>
<tr>
<th>参数</th>
<th> 参数意义</th>
</tr>
</thead>
<tbody>
<tr>
<td><strong>-F<br>--onefile</strong></td>
<td>1.打包单个文件,产生一个文件用于部署(默认),如果代码都写在一个.py文件时使用,项目有多个文件时不要使用<br><br>例:pyinstaller -F xxx.py<br>例:pyinstaller --onefile xxxx.py</td>
</tr>
<tr>
<td><strong>-D<br>--onedir</strong></td>
<td>1.打包多个文件,产生一个目录用于部署(默认),用于框架编写的代码打包<br><br>例:pyinstaller -D xxx.py(项目入口文件)<br>例:pyinstaller --onedir xxx.py(项目入口文件)</td>
</tr>
<tr>
<td><strong>--key=keys</strong></td>
<td>1.使用keys进行加密打包<br><br>例:pyinstaller --key=1234 -F xx.py</td>
</tr>
<tr>
<td><strong>-K<br>--tk</strong></td>
<td>1.在部署时包含 TCL/TK</td>
</tr>
<tr>
<td><strong>-a<br>--ascii</strong></td>
<td>1.不包含编码.在支持Unicode的python版本上默认包含所有的编码</td>
</tr>
<tr>
<td><strong>-d<br>--debug</strong></td>
<td>1.产生debug版本的可执行文件</td>
</tr>
<tr>
<td><strong>-n name<br>--name=name</strong></td>
<td>1.可选的项目(产生的spec的)名字name<br>2.第一个脚本的主文件名将作为spec的名字(默认)<br><br>例:pyinstaller -F -n my_file_namexxx.py<br>例:pyinstaller -F --name=my_file_namexxx.py</td>
</tr>
<tr>
<td><strong>-o dir<br>-- out=dir</strong></td>
<td>1.指定spec文件的生成目录dir<br>2.如果没有指定且当前目录是PyInstaller的根目录,会自动创建一个用于输出(spec和生成的可执行文件)的目录<br>3.如果没有指定切当前目录不是PyInstaller的根目录,则会输出到当前的目录下</td>
</tr>
<tr>
<td><strong>-p dir<br>--path=dir</strong></td>
<td>1.用来添加程序所用到的包的所在位置,设置导入路径(和使用pythonpath效果相似)<br>2.可以用路径分割符(Windows使用分号,Linux使用冒号)分割,指定多个目录.也可以使用多个-p参数来设置多个导入路径,让Pyintaller自己去找程序需要的资源</td>
</tr>
<tr>
<td><strong>-w<br>--windowed<br>--noconsole</strong></td>
<td>1.表示去掉控制台窗口,使用Windows子系统执行,当程序启动的时候不会打开命令行(只对Windows有效)<br><br>例:pyinstaller -c xxx.py<br>例:pyinstaller xxx.py --noconsole</td>
</tr>
<tr>
<td><strong>-c<br>--nowindowed<br>--console</strong></td>
<td>1.表示打开控制台窗口,使用控制台子系统执行,当程序启动的时候会打开命令行(默认)(只对Windows有效)<br><br>例:pyinstaller -c xxx.py<br>例:pyinstaller xxx.py --console</td>
</tr>
<tr>
<td><strong>-i<br>--icon=&lt;file.ioc&gt;</strong></td>
<td>1.将file.ico添加为可执行文件的资源,改变程序的图标(只对Windows系统有效)<br><br>例:pyinstaller -F -i file.icoxxx.py<br>例:pyinstall -F --icon=&lt;file.ioc&gt; xxx.py</td>
</tr>
<tr>
<td><strong>--icon=&lt;file.exe,n&gt;</strong></td>
<td>1.将file.exe的第n个图标添加为可执行文件的资源(只对Windows系统有效)</td>
</tr>
<tr>
<td><strong>-v file<br>--version=file</strong></td>
<td>1.将verfile作为可执行文件的版本资源(只对Windows系统有效)</td>
</tr>
<tr>
<td><strong>-s<br>--strip</strong></td>
<td>1.可执行文件和共享库将run through strip.注意Cygwin的strip往往使普通的win32 Dll无法使用</td>
</tr>
<tr>
<td><strong>-X<br>--upx</strong></td>
<td>1.如果有UPX安装(执行Configure.py时检测),会压缩执行文件(Windows系统中的DLL也会)(参见note)</td>
</tr>
</tbody>
</table>
<h4 id="三pyinstaller的使用">三、Pyinstaller的使用</h4>
<ul>
<li>PyInstaller在Windows/Linux/Mac环境下的使用:<strong>执行命令相同,只需要在不同环境下执行即可</strong></li>
</ul>
<h4 id="1pyinstaller的安装">1.pyinstaller的安装</h4>
<pre><code class="language-python">pip install pyinstaller
</code></pre>
<h4 id="2执行命令">2.执行命令</h4>
<ul>
<li>2.1打包单个文件</li>
</ul>
<pre><code class="language-python"># 1.执行命令
pyinstaller -F xxx.py

# 2.去生成的dist文件夹找xxx.exe运行

# 3.运行成功,xxx.exe则为可执行文件,删除其它文件
</code></pre>
<ul>
<li>2.2打包多个文件</li>
</ul>
<pre><code class="language-python"># 1.执行命令,xxx.py为程序入口文件
pyinstall -D xxx.py

# 2.删除生成的bulid和dist文件夹,仅保留xxx.spec文件

# 3.修改xxx.spec文件,详见2.2.1

# 4.执行命令
pyinstaller -F xxx.spec

# 5.去dist文件夹下找xxx.exe文件

# 6.运行成功,删除临时文件目录build;dist目录为打包的结果,可执行文件和其它程序运行的关联文件都在这个目录下
</code></pre>
<ul>
<li>2.2.1 xxx.spec配置文件详解</li>
</ul>
<pre><code class="language-python"># -*- mode: python -*-

block_cipher = None

# 以py文件为输入,分析py文件的依赖模块,并生成相应的信息
a = Analysis(['xxx.py'], # 要打包.py文件名列表,和xxx.py同级可以不同添加
       pathex=['D:\\abc\\def\\project_v1.0'], # 项目路径
       binaries=[], # 程序调用外部pyd、dll文件(二进制文件路径)以数组形式传入;例:('D:\\pro\\text.dll', 'pro'),将'pdftotext.dll'pro,与原项目结构一致即可
       datas=[], # 存放的资源文件(图片、文本等静态文件)以数组形成传入;例:('D:\\static\\c.ioc','static'),将'cc.ioc'打包之后放在static目录,与原项目结构一致即可
       hiddenimports=[], # pyinstaller解析模块时可能会遗漏某些模块(not visible to the analysis phase),造成打包后执行程序时出现类似No Module named xxx;这时就需要在hiddenimports中加入遗漏的模块
       hookspath=[],
       runtime_hooks=[],
       excludes=[], # 去除不必要的模块import,写在excludes中添加此模块
       win_no_prefer_redirects=False,
       win_private_assemblies=False,
       cipher=block_cipher)

# .pyz的压缩包,包含程序运行需要的所有依赖
pyz = PYZ(a.pure, a.zipped_data,
       cipher=block_cipher)
      
# 根据Analysis和PYZ生成单个exe程序所需要的属性及其配置
exe = EXE(pyz,
   a.scripts,
   exclude_binaries=True,
   name='xxx', # 生成exe文件的名字
   debug=False, # debug模式
   strip=False,
   upx=True,
   console=False, # 是否在打开exe文件时打开cmd命令框
   icon='C:\Users\xx.ico' ) # 设置exe程序图标,ico格式文件(16*16)
   
# 收集前三个部分的内容进行整合,生成程序所需要的依赖包,及资源文件和配置
coll = COLLECT(exe,
      a.binaries,
      a.zipfiles,
      a.datas,
      strip=False,
      upx=True,
      name='fastplot')
</code></pre>
<h4 id="四踩坑">四、踩坑</h4>
<ul>
<li>编辑.spec文件<strong>路径相关</strong></li>
</ul>
<blockquote>
<p>1.windows尽量使用绝对路径,用双斜杠\ \</p>
<p>2.linux路径/home/my_project/web</p>
<p>3..路径避免使用中文</p>
</blockquote>
<ul>
<li>打包.spec文件报错:<strong>RecursionError: maximum recursion depth exceeded</strong></li>
</ul>
<blockquote>
<p>1.在spec文件上添加递归深度的设置</p>
<p>import sys</p>
<p>sys.setrecursionlimit(5000)</p>
</blockquote>
<ul>
<li>更换exe图标报错:<strong>AttributeError: module 'win32ctypes.pywin32.win32api' has no attribute 'error'</strong></li>
</ul>
<blockquote>
<p>1.图标的大小建议(64*64): https://lvwenhan.com/convertico/</p>
<p>2.图标的颜色严格限制:256,真彩色是不行的</p>
</blockquote>
<ul>
<li>打包错误:<strong>ModuleNotFoundError: No module named 'xxxxx'</strong></li>
</ul>
<blockquote>
<p>方法1:pyinstaller -D --hidden-import="xxxxx" main.py</p>
<p>方法2:在xxx.spec中配置hiddenimports=['xxxxx']</p>
</blockquote>
<ul>
<li>运行exe文件报错:<strong>Failed to excute Script main</strong></li>
</ul>
<blockquote>
<p>1.使用-c模式重新打包调试,找的缺失的模块,pip install安装</p>
</blockquote>
<ul>
<li>文件打包后过大</li>
</ul>
<blockquote>
<p>1.在程序中尽量不使用import xx;而是使用 from xx import xx</p>
</blockquote><br><br>
来源:https://www.cnblogs.com/bbiu/p/13209612.html
頁: [1]
查看完整版本: PyInstaller打包Python项目详解