1. # 安装打包工具
  2. pip3 install PyInstaller

如果用户使用pyenv管理python版本则会与pyinstaller产生冲突。这是由于pyinstaller需要framework构建的CPython

  1. env PYTHON_CONFIGURE_OPTS="--enable-framework" pyenv install 3.9.10
  2. # OR
  3. env PYTHON_CONFIGURE_OPTS="--enable-shared" pyenv install3.9.10

由于某些特殊原因,这种安装方式往往会下载失败所以首先需要给终端装上弹射座椅:

  1. vim ~/.zshrc
  2. ## 添加弹射椅配置,端口的配置取决于梯子的实际配置
  3. export http_proxy="sock5://127.0.0.1:1080"
  4. export https_proxy="socks5://127.0.0.1:1080"
  5. echo "Socks Proxy on"
  6. # 测试
  7. curl myip.ipip.net
  8. # 地址为大陆之外的地区即可
  1. # 打包
  2. pyinstaller -n "APP名称" --windowed app.py

打包成功后,在dist文件夹中可以找到该文件。双击点开即可使用。

ICON 制作

事先准备好一个1024*1024像素的图片,然后进行剪辑压缩到文件夹tmp.iconset

  1. sips -z 16 16 2x.png --out tmp.iconset/icon_16x16.png
  2. sips -z 32 32 2x.png --out tmp.iconset/icon_16x16@2x.png
  3. sips -z 32 32 2x.png --out tmp.iconset/icon_32x32.png
  4. sips -z 64 64 2x.png --out tmp.iconset/icon_32x32@2x.png
  5. sips -z 128 128 2x.png --out tmp.iconset/icon_128x128.png
  6. sips -z 256 256 2x.png --out tmp.iconset/icon_128x128@2x.png
  7. sips -z 256 256 2x.png --out tmp.iconset/icon_256x256.png
  8. sips -z 512 512 2x.png --out tmp.iconset/icon_256x256@2x.png
  9. sips -z 512 512 2x.png --out tmp.iconset/icon_512x512.png
  10. sips -z 1024 1024 2x.png --out tmp.iconset/icon_512x512@2x.png
  11. # 然后基于该文件夹生成icns文件
  12. iconutil -c icns tmp.iconset -o Icon.icns
  13. # 生成带有ICON的app
  14. pyinstaller --name "YOUR_APP_NAME" --icon "Icon.icns" --windowed app.py

图片资源的导入

在开发环境中我们可以使用相对路径引入图片,但是这种方式在打包后静态资源会丢失。所以需要使用OS级别的绝对路径导入图片。

  1. import os
  2. basedir = os.path.dirname(__file__)
  3. button1.setIcon(QIcon(os.path.join(basedir, "icons", "hand.png")))

创建磁盘镜像

  1. brew install create-dmg
  2. # 创建脚本
  3. touch builddmg.sh

代码如下:

  1. #!/bin/sh
  2. # Create a folder (named dmg) to prepare our DMG in (if it doesn't already exist).
  3. mkdir -p dist/dmg
  4. # Empty the dmg folder.
  5. rm -r dist/dmg/*
  6. # Copy the app bundle to the dmg folder.
  7. cp -r "dist/头文字Z.app" dist/dmg
  8. # If the DMG already exists, delete it.
  9. test -f "dist头文字Z.dmg" && rm "dist/头文字Z.dmg"
  10. create-dmg \
  11. --volname "头文字Z" \
  12. --volicon "Icon.icns" \
  13. --window-pos 200 120 \
  14. --window-size 600 300 \
  15. --icon-size 100 \
  16. --icon "头文字Z.app" 50 50 \
  17. --hide-extension "头文字Z.app" \
  18. --app-drop-link 425 120 \
  19. "dist/头文字Z.dmg" \
  20. "dist/dmg/"
  1. chmod +x builddmg.sh
  2. ./builddmg.sh