转自: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. kubectl命令详解

    一.kubectl 基本命令 1.陈述式资源管理方法: 1.kubernetes集群管理集群资源的唯一入口是通过相应的方法调用apiserver的接口 2.kubectl 是官方的CLI命令行工具,用 ...

  2. 第3章---数据探索(python数据挖掘)

    1.缺失值分析及箱型图 数据:catering_sale.xls(餐饮日销额数) 缺失值使用函数:describe()函数,能算出数据集的八个统计量 import pandas as pd cater ...

  3. 获取指定网卡的ip

    #!/usr/bin/env python # -*- coding: utf-8 -*- import socket import fcntl import struct def get_ip_ad ...

  4. spring下的restTemplate使用

    首先上配置,由于restTemplate不支持一些返回格式,所以需要自己手动配置 /** * @Description : restTemplate配置类 * @Author : wzkris * @ ...

  5. 使用 IntersectionObserver API 遇到的一些问题

    root 设指定为 document.body 时不会触发更新 See the Pen document.body and IntersectionObserver by y1j2x34 (@y1j2 ...

  6. STM32的USART的DMA不定长度收发代码

    /* * 函数名:USART1_DMA_Config * 描述  :串口1的DMA 的初始化配置 * 输入  :无 * 输出  : 无 * 调用  :在USART1_INIT()中被调用 */ voi ...

  7. angularJS:一个页面多个ng-app

    var app = angular.module('myApp', []); app.controller('myCtrl', function($scope, $rootScope) { $scop ...

  8. python通过轮子安装第三方库(以Wordcloud为例)

    1.查看python版本 直接输入如下命令: python 执行结果如下: 我们可以直到,本机的python版本为: AMD64bit 3.11版本python 2.下载合适python版本的轮子 下 ...

  9. Android笔记--监听短信内容

    监听短信内容 就比如说是在我们用一个软件需要使用"获取验证码"的功能时,能够跟短信的验证码互通,实现较为完整的登录功能: 监听短信内容主要是利用了contentObserver实现 ...

  10. RTC 科普视频丨聊聊空间音频的原理与其背后的声学原理

    在现在很多的线上实时互动场景中,我们重视的不仅仅是互动体验,还要提升沉浸感.而在很多场景中,仅凭空间音频技术,就可以带来如临其境的体验.空间音频技术的原理是怎样的呢? 看过我们新一期的 RTC 科普视 ...