转自:https://www.jianshu.com/p/48f6dea265eb

pyinstaller简明教程

安装pyinstaller

pip install pyinstaller是最简单的安装方式,但也常常由于各种原因报错,这就需要我们通过whl文件来下载,但是whl文件安装也有考究,具体参考我之前的教程:Python安装whl文件那些坑,下载whl一般可以在whl文件仓库中找到,如果找不到就去第三方库的官网下载,我个人比较倾向于第二种。

这是我最后一次阐述pip install的问题,后续的教程都不再赘述

pyinstaller基本用法

假如我们要打包一个demo.py文件,基本过程是:

打开cmd,并切换到demo.py文件所在的目录,注意路径中不要有中文
执行命令:pyinstaller demo.py
在当前的目录下,将会生成两个文件夹:build和dist。dist里面就是所有可执行文件,点击demo.exe就能运行了。

pyinstaller指令的常见可选参数:

可选参数 格式举例 功能说明
-F pyinstaller -F demo.py 只在dist中生产一个demo.exe文件。
-D pyinstaller -D demo.py 默认选项,除了demo.exe外,还会在在dist中生成很多依赖文件,推荐使用。
-c pyinstaller -c demo.py 默认选项,只对windows有效,使用控制台,就像编译运行C程序后的黑色弹窗。
-w pyinstaller -w demo.py 只对windows有效,不使用控制台。
-p pyinstaller -p E:\python\Lib\site-packages demo.py 设置导入路径,一般用不到。
-i pyinstaller -i D:\file.icon demo.py 将file.icon设置为exe文件的图标,推荐一个icon网站:icon

上面的可选参数可以组合使用,比如pyinstaller -F -i D:\file.icon demo.py
能够from xxx import yyy就尽量不要import xxx,这样可以减少打包后的体积。

pyinstaller高阶功法

一般而言,pyinstaller的基本用法已经够用了,但是有特殊需求,比如打包图片资源文件时,就必须用到它的高阶功法了。

首先得了解spec文件,简而言之,spec文件就是一份告诉pyinstaller如何打包的配置文件。
可以通过pyi-makespec demo.py来生成demo.spec文件。其内容如下:

# -*- mode: python -*-

block_cipher = None

resources = (("inspurer.db", "."), ("dlib_face_recognition_resnet_model_v1.dat", "."),
("shape_predictor_68_face_landmarks.dat", "."), ("close_logcat.png", ".")
, ("open_logcat.png", "."), ("finish_register.png", "."), ("new_register.png", ".")
, ("start_punchcard.png", "."), ("end_puncard.png", "."), ("index.png", ".")) a = Analysis(['workAttendanceSystem.py'],
pathex=['C:\\Users\\lenovo\\Desktop\\test\\python'],
binaries=[],
datas=resources,
hiddenimports=[],
hookspath=[],
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher,
noarchive=False)
pyz = PYZ(a.pure, a.zipped_data,
cipher=block_cipher)
exe = EXE(pyz,
a.scripts,
[],
exclude_binaries=True,
name='workAttendanceSystem',
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=True,
console=True )
coll = COLLECT(exe,
a.binaries,
a.zipfiles,
a.datas,
strip=False,
upx=True,
name='workAttendanceSystem')

对于上面这个文件,需要注意两点:

除了resources配置是我添加修改之外,其余全是自动生成,这个配置是用来添加资源文件的。
pathex是工程的根目录。

生成并配置好spec文件后,我们可以通过pyinstaller demo.spec来执行打包任务。

python利用pyinstaller打包简明教程的更多相关文章

  1. 利用PyInstaller打包exe文件

    前言 平常我们通过Python写完一些小脚本之后,如果使用不频繁的话,一般会选择在DOS界面直接跑脚本,或者在IDE中运行.但当我们需要频繁使用某些脚本,或者在没有Python环境的机器上也能顺利运行 ...

  2. 预测球队比赛结果及利用pyinstaller打包文件

    一.预测乒乓球球队比赛成绩 1.乒乓球比赛规则 一局比赛:在一局比赛中,先得11分的一方为胜方:10平后,先多得2分的一方为胜方. 一场比赛:单打的淘汰赛采用七局四胜制,双打淘汰赛和团体赛采用五局三胜 ...

  3. python用pyinstaller打包成exe文件

    版本为Python2.7 一.安装Pyinstaller 1.安装pywin32 下载安装文件:查找到跟自己适用的python版本及window系统版本匹配的pywin32,下载后安装  使用pip命 ...

  4. 【Python】pyinstaller打包运行报错failed to execute script main

    前言 最近用pyinstaller打包的时候一直报"failed to execute script main". 最终使用"pyinstaller --hidden-i ...

  5. python 使用pyinstaller打包程序

    使用pyinstaller 打包.py脚本,在其他计算机可以直接运行,不需要python环境 安装pyinstaller库 pip install pystaller 打包程序 pyinstaller ...

  6. 利用pyinstaller 打包Python文件

    1.下载安装pyinstaller模块 cmd 命令: pip install pyinstaller cmd命令: pip list 查看自己安装的模块 2.建议把要大包的Python文件单独放到新 ...

  7. Python | 用Pyinstaller打包发布exe应用

    参考:https://jingyan.baidu.com/article/a378c960b47034b3282830bb.html https://ask.csdn.net/questions/72 ...

  8. python使用Pyinstaller打包

    一.前言 python文件打包,将.py文件转化成.exe文件(windows平台),可以使用Pyinstaller来打包 Pyinstaller可以在全平台下使用,但是请注意打包生成的文件不能在全平 ...

  9. Python 函数装饰器简明教程

    定义类的静态方法时,就使用了装饰器.其实面向对象中的静态方法都是使用了装饰器. @staticmethod def jump(): print(" 3 meters high") ...

  10. python pyinstaller 打包exe报错

    今天用python 使用pyinstaller打包exe出现错误 环境pyqt5 + python3.6 在导入pyqt5包之前加上如下代码 import sysimport osif hasattr ...

随机推荐

  1. 运行python脚本报错SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape

    运行python脚本报错 SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes  in position 2-3: ...

  2. Delphi数据库备份

    此处代码只是测试代码,仅仅是测试 //环境:D7+SQL Server 2008 1 unit Unit1; 2 3 interface 4 5 uses 6 Windows, Messages, S ...

  3. beamforming源码标记

    p:各阵元的声压信号矩阵 R:接收数据的自协方差矩阵 Pcbf:交叉谱矩阵

  4. Github高效搜索方式

    Github高效搜索方式 文章目录 Github高效搜索方式 0.写在前面 1.常用的搜索功能 1.1 直接搜索 1.2 寻找指定用户|大小的仓库 1.3 搜索仓库 1.4 查找特定star范围的仓库 ...

  5. Codeforces Round #803 (Div. 2) A-D 刚vp完还没补题

    Codeforces Round #803 (Div. 2) 2022/7/24 上午VP 传送门:https://codeforces.com/contest/1698 A. XOR Mixup 随 ...

  6. VS 2022创建ATL组件 (C++)

    https://www.cnblogs.com/chechen/p/8119018.html 步骤如下: 1.新建ATL项目 打开Visual Studio 2022 新建ATL项目 2.添加接口类. ...

  7. sqlserver数据库连接数相关问题

    sql语句监控连接数量 SELECT ec.client_net_address , es.[program_name] , es.[host_name] , es.login_name , COUN ...

  8. 【Python】Python3环境安装

    编译安装 安装依赖 yum install wget gcc make zlib-devel openssl openssl-devel readline-devel wget "https ...

  9. P2962 [USACO09NOV]Lights G(Meet In The Middle)

    [USACO09NOV]Lights G 题目描述 给出一张n个点n条边的无向图,每个点的初始状态都为0. 你可以操作任意一个点,操作结束后该点以及所有与该点相邻的点的状态都会改变,由0变成1或由1变 ...

  10. Javaweb学习笔记第十一弹(内含Servlet相关知识呦!)

    Web核心 静态资源:HTML,CSS,JavaScript,图片等,负责页面展现 动态资源:Servlet,JSP等,负责逻辑处理 数据库:负责存储数据 HTTP协议:定义通信规则 Web服务器:负 ...