pyinstaller使用

pyinstaller 入门

  • Pyat5 的安装程序开发
  • PyQt6 的安装程序开发
    1. 编写好的程序编译成可执行文件+资源文件:
    2. 用 zip 打包,基本可以压缩到 1/3 大小;
    3. 然后再用 pyqt 写一个 setup 安装程序,安装到指定目录(安装的过程实际就是把文件解压、拷贝到指定目录、注册到注册表中)

pyinstaller 简介

PyInstaller bundles a Python application and all its dependencies into a singlepackage.The user can run the packaged app without installing a Pythoninterpreter or any modules. Pyinstaller supports Python 3.7 and newer, andcorrectly bundles many major Python packages such as numpy, matplotlib, PyQtwxPython, and others.

PyInstaller is tested against Windows, MacOS X, and Linux. However, it is not across-compiler: to make a Windows app vou run PyInstaller on Windows, and tomake a tinux app you cor it on linps, etc, ioyinsts ler has beer asedst them isnot part of our continuous integration tests, and the development team offersno guarantee (all code for these platforms comes from external contributionsthat PyInstaller will work on these platforms or that they will continue to besupported.)

安装

pip install pyinstaller -i https://pypi.douban.com/simple

官网

使用

进入到 pyinstaller 目录下,执行命令:

shell 复制代码
pyinstaller -F main.py

参数:

  • -F: 打包成一个 exe 文件
  • -w: 打包成一个无控制台窗口的 exe 文件
  • -i: 打包成带图标的 exe 文件(后面跟图标文件路径)
  • -n: 打包成指定名称的 exe 文件(后面跟文件名)
  • -D: 打包成一个文件夹,里面包含 exe 文件和其他文件

打包后的文件

  • dist 文件夹: 包含打包后的文件
  • main.spec 文件: 包含打包的配置信息
  • main.exe 文件: 打包后的可执行文件
选项 说明
-h, --help 查看该模块的帮助信息
-F, --onefile 产生单个的可执行文件
-D, --onedir 产生一个目录(包含多个文件)作为可执行程序
-a, --ascii 不包含 Unicode 字符集支持
-d, --debug 产生 debug 版本的可执行文件
-w, --windowed, --noconsole 指定程序运行时不显示命令行窗口(仅对 Windows 有效)
-c, --nowindowed, --console 指定使用命令行窗口运行程序(仅对 Windows 有效)
-o DIR, --out=DIR 指定 spec 文件的生成路径。如果未指定,则默认使用当前目录来生成 spec 文件
-p DIR, --path=DIR 设定 Python 导入模块的路径(和设置 PYTHONPATH 环境变量的作用相似)。也可使用路径分隔符(Windows 使用分号,Linux 使用冒号)来分隔多个路径
-n NAME, --name=NAME 指定项目(产生的 spec)的名字。如果省略该选项,那么第一个脚本文件的主文件名将作为 spec 的名字

pyqt 打包后的图标丢失问题

在使用 pyinstaller 打包 pyqt 程序时,如果程序中使用了图标,打包后的程序的图标会丢失。

解决方法:

  1. 把图标文件夹复制放到程序所在目录下,并在程序中指定图标路径。
  2. 编译完成后修改.spec 文件,在其中添加图标文件路径(双斜杠)
相关推荐
vibag7 分钟前
LangSmith监控
人工智能·python·语言模型·langchain·大模型
YJlio28 分钟前
PsPing 学习笔记(14.7):一条龙网络体检脚本——连通性、延迟、带宽全都要
开发语言·网络·笔记·python·学习·pdf·php
2401_8414956438 分钟前
【DeepSeek系列】论文《mHC: Manifold-Constrained Hyper-Connections》全流程复现详解(附Python代码)
人工智能·pytorch·python·深度学习·论文复现·deepseek·mhc模型
汤姆yu39 分钟前
基于python大数据的地震数据可视化分析系统
大数据·python·信息可视化
阿蔹44 分钟前
Python-基础语法五-数据可视化、对象、类、多态、继承、封装、抽象类
开发语言·python
小白学大数据1 小时前
未来趋势:AI 时代下 python 爬虫技术的发展方向
运维·人工智能·爬虫·python·自动化
dagouaofei1 小时前
2026 年年度工作计划 PPT:AI 自动生成方案横向对比
人工智能·python·powerpoint
reasonsummer1 小时前
【教学类-70-06】20260104“折纸-花纹镜”(花纹图案+上下打孔+无学号框)小2班第一次尝试
python·通义万相
YJlio1 小时前
PsPing 学习笔记(14.8):常见错误与排障实战(超时、拒绝连接、权限问题)
开发语言·笔记·python·学习·django·pdf·pygame
wjykp1 小时前
part6 PyTorch
人工智能·pytorch·python