打包Python文件-PyInstaller

  • PyInstaller 的基本用法

    pyinstaller script.py
    pyinstaller -D script.py  # 默认模式
    pyinstaller --onedir script.py # 默认值
    
    • 前提:有python文件 script.py

    • 生成:

      • dist/script/:打包后的可执行文件所在目录(默认是单目录模式)
      • build/:构建过程中的临时文件
      • script.spec:打包配置文件(可修改后重新打包)
  • 打包成单个可执行文件

    pyinstaller -F app.py
    pyinstaller --onedir script.py
    
    • 生成:

      • dist/script.exe
      • build/:构建过程中的临时文件
  • 参数说明

    参数含义
    -F--onefile打包成单个可执行文件(而不是一个文件夹)
    -w不显示命令行窗口(适用于 GUI 程序)
    -i icon.ico设置可执行文件的图标
    --add-data "src;dest"添加非代码资源文件(Windows 用分号 ;,Linux/macOS 用冒号 :
    --hidden-import module手动指定未自动识别的模块
    -n NAME设置生成的可执行文件名称
  • 打包一个带图标的 GUI 程序为单文件

    pyinstaller -F -w -i myicon.ico my_gui_app.py
    
posted @ 2025-11-11 21:01  stars-AI  阅读(1)  评论(0)    收藏  举报  来源