关于使用 nuitka进行构建python应用的一些配置,以及github action自动构建;

发布于:2025-04-18 ⋅ 阅读:(26) ⋅ 点赞:(0)

1. 通用配置

# 设置输出目录和文件名
    output_dir = "dist"
    app_name = "CursorAutoFree"

    # 基础命令行选项
    base_options = [
        "--follow-imports",  # 跟踪导入
        "--enable-plugin=tk-inter",  # 启用 Tkinter 支持
        "--include-package=customtkinter",  # 包含 customtkinter
        f"--include-data-dir=src/utils/turnstilePatch=turnstilePatch",  # 包含数据目录
        f"--include-data-files=src/core/names-dataset.txt=names-dataset.txt",  # 包含具体的 txt 文件
        "--include-data-dir=src/config=src/config",  # 包含配置目录
        "--show-memory",  # 显示内存使用
        "--show-progress",  # 显示进度
        "--nofollow-import-to=tkinter.test",  # 排除测试模块
        "--nofollow-import-to=PIL.ImageQt",  # 排除 Qt 相关
        "--remove-output",  # 删除之前的输出
        f"--output-dir={output_dir}",  # 输出目录
        f'--output-file="{app_name}"',  # 输出文件名
    ]

2.macos

 [
                "--macos-create-app-bundle",  # macOS 创建 .app(默认就是 GUI 模式)
                "--macos-app-icon=src/assets/app_icon.icns",  # 添加 macOS 应用图标
                f'--macos-app-name="{app_name}.app"',  # macOS应用名称
            ]

3. windows

 [
                "--windows-console-mode=disable",  # Windows 禁用控制台
                "--windows-icon-from-ico=src/assets/app_icon.ico",  # Windows 应用图标
            ]

使用 github action 配置自动打包(目前只做了windows)

这里讲一下,nuitka 打包真的很慢;不过打包的应用启动的还挺快

build-windows:
    runs-on: windows-latest
    
    steps:
      - uses: actions/checkout@v4
      
      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: '3.x'
          architecture: 'x64'
          cache: 'pip'
          cache-dependency-path: |
            **/requirements*.txt
      
      - name: Install Dependencies
        run: |
          pip install -r requirements.txt
      
      - name: Build Windows Executable
        uses: Nuitka/Nuitka-Action@main
        with:
          nuitka-version: main
          script-name: CursorAutoFree.py
          mode: onefile
          enable-plugins: tk-inter
          include-package: customtkinter
          include-data-dir: |
            src/utils/turnstilePatch=turnstilePatch
            src/config=src/config
          include-data-files: src/core/names-dataset.txt=names-dataset.txt
          windows-console-mode: disable
          windows-icon-from-ico: src/assets/app_icon.ico
          output-filename: CursorAutoFree
          
      - name: Upload Windows artifact
        uses: actions/upload-artifact@v4
        with:
          name: CursorPro-Windows
          path: build/
          include-hidden-files: true


网站公告

今日签到

点亮在社区的每一天
去签到