这个“.spec”文件为 macOS 生成捆绑包有什么问题?

问题描述 投票:0回答:1

我正在尝试为 此项目生成适用于 macOS 的捆绑包。存储库中的

.spec
文件在 Windows 上运行良好,但当我尝试在 macOS 上使用它时,它失败了。

我尝试通过修改它来修复MacOS,结果如下:

# -*- mode: python ; coding: utf-8 -*-
import os.path
from pathlib import Path
from PyInstaller.compat import is_darwin, is_win
from PyInstaller.utils.hooks import copy_metadata, collect_data_files, collect_all


datas = []

datas += collect_data_files("transformers", include_py_files=True)
datas += collect_data_files("lightning")
datas += collect_data_files("lightning_fabric")
datas += collect_data_files("speechbrain")
datas += collect_data_files("pyannote")
datas += collect_data_files("asteroid_filterbanks")
datas += collect_data_files("whisperx")
datas += collect_data_files("librosa")

datas += copy_metadata("tqdm")
datas += copy_metadata("regex")
datas += copy_metadata("requests")
datas += copy_metadata("packaging")
datas += copy_metadata("filelock")
datas += copy_metadata("numpy")
datas += copy_metadata("tokenizers")
datas += copy_metadata("pillow")
datas += copy_metadata("huggingface_hub")
datas += copy_metadata("safetensors")
datas += copy_metadata("pyyaml")

datas += [("res", "res")]
datas += [("config.ini", ".")]
datas += [(".env", ".")]
datas += [("venv/lib/python3.10/site-packages/customtkinter", "customtkinter")]
datas += [("venv/lib/python3.10/site-packages/torch", "torch")]
datas += [("venv/lib/python3.10/site-packages/torchgen", "torchgen")]
datas += [("venv/lib/python3.10/site-packages/speech_recognition", "speech_recognition")]
datas += [("venv/lib/python3.10/site-packages/moviepy", "moviepy")]
datas += [("venv/lib/python3.10/site-packages/dotenv", "dotenv")]
datas += [("venv/lib/python3.10/site-packages/openai", "openai")]

hiddenimports = [
    "huggingface_hub.repository",
    "pytorch",
    "sklearn.utils._cython_blas",
    "sklearn.neighbors.typedefs",
    "sklearn.neighbors.quad_tree",
    "sklearn.tree",
    "sklearn.tree._utils",
    "darkdetect",
    "tkinter.font",
    "tkinter.ttk",
    "cmath",
    "pickletools",
    "unittest.mock",
    "aifc",
    "proglog",
    "imageio",
    "decorator",
]

block_cipher = None

debug = True
if debug:
    options = [("v", None, "OPTION")]
else:
    options = []

a = Analysis(
    ["src/app.py"],
    pathex=[],
    binaries=[],
    datas=datas,
    hiddenimports=hiddenimports,
    hookspath=[],
    hooksconfig={},
    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)

macos_icon = "res/macos/icon.icns"
windows_icon = "res/windows/icon.ico"

exe = EXE(
    pyz,
    a.scripts,
    options,
    exclude_binaries=True, #NEW
    name="Audiotext",
    debug=debug,
    bootloader_ignore_signals=False,
    strip=False,
    upx=True,
    upx_exclude=[],
    runtime_tmpdir=None,
    console=debug,
    disable_windowed_traceback=False,
    argv_emulation=False,
    target_arch=None,
    codesign_identity=None,
    entitlements_file="res/macos/entitlements.plist" if is_darwin else None,
    icon=windows_icon if is_win else macos_icon,
)

coll = COLLECT(
    exe,
    a.binaries,
    a.zipfiles,
    a.datas,
    strip=False,
    upx=True,
    upx_exclude=[],
    name="Audiotext",
)

app = BUNDLE(
    coll,
    name="Audiotext.app",
    icon=macos_icon,
    bundle_identifier="com.henestrosadev.audiotext",
    version="2.3.0",
    info_plist={
        "NSPrincipalClass": "NSApplication",
        "NSAppleScriptEnabed": False,
        "NSHighResolutionCapable": True,
        "NSMicrophoneUsageDescription": "Allow Audiotext to record audio from your microphone to generate transcriptions.",
    }
)

现在,它生成了捆绑包,但是当我打开生成的文件时,我总是得到一个

ModuleNotFoundError
。例如,我现在的错误是
ModuleNotFoundError: No module named 'openai'
。我通过添加这一行来修复它,就像我对之前丢失的模块的其余部分所做的那样:

datas += [("venv/lib/python3.10/site-packages/openai", "openai")]

现在,我得到的错误是

ModuleNotFoundError: No module named 'pydantic'
。出了点问题,因为手动添加所有程序的包是没有意义的。

python macos pyinstaller
1个回答
0
投票

问题与

pathex
有关,因为由于某种原因PyInstaller只获取了项目的
src
目录。我将
site-packages
目录从
venv
目录添加到
pathex

文件现在如下:

# -*- mode: python ; coding: utf-8 -*-
import os.path
from pathlib import Path
from PyInstaller.compat import is_darwin, is_win

import sys ; sys.setrecursionlimit(sys.getrecursionlimit() * 5)

relative_site_packages_path = "venv/Lib/site-packages" if is_win else "venv/lib/python3.10/site-packages"

datas = [
    (f"{relative_site_packages_path}/customtkinter", "customtkinter"),
    (f"{relative_site_packages_path}/transformers", "transformers"),
    (f"{relative_site_packages_path}/lightning", "lightning"),
    (f"{relative_site_packages_path}/lightning_fabric", "lightning_fabric"),
    (f"{relative_site_packages_path}/speechbrain", "speechbrain"),
    (f"{relative_site_packages_path}/pyannote", "pyannote"),
    (f"{relative_site_packages_path}/asteroid_filterbanks", "asteroid_filterbanks"),
    (f"{relative_site_packages_path}/whisperx", "whisperx"),
    (f"{relative_site_packages_path}/librosa", "librosa"),
    ("res", "res"),
    ("config.ini", "."),
    (".env", "."),
]

hiddenimports = [
    "huggingface_hub.repository",
    "sklearn.utils._cython_blas",
    "sklearn.neighbors.quad_tree",
    "sklearn.tree",
    "sklearn.tree._utils",
]

block_cipher = None

debug = False
if debug:
    options = [("v", None, "OPTION")]
else:
    options = []

a = Analysis(
    ["src/app.py"],
    pathex=[relative_site_packages_path],
    binaries=[],
    datas=datas,
    hiddenimports=hiddenimports,
    hookspath=[],
    hooksconfig={},
    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)

macos_icon = "res/macos/icon.icns"
windows_icon = "res/windows/icon.ico"

exe = EXE(
    pyz,
    a.scripts,
    options,
    exclude_binaries=True, #NEW
    name="Audiotext",
    debug=debug,
    bootloader_ignore_signals=False,
    strip=False,
    upx=True,
    upx_exclude=[],
    runtime_tmpdir=None,
    console=debug,
    disable_windowed_traceback=False,
    argv_emulation=False,
    target_arch=None,
    codesign_identity=None,
    entitlements_file="res/macos/entitlements.plist" if is_darwin else None,
    icon=windows_icon if is_win else macos_icon,
)

coll = COLLECT(
    exe,
    a.binaries,
    a.zipfiles,
    a.datas,
    strip=False,
    upx=True,
    upx_exclude=[],
    name="Audiotext",
)

app = BUNDLE(
    coll,
    name="Audiotext.app",
    icon=macos_icon,
    bundle_identifier="com.henestrosadev.audiotext",
    version="2.3.0",
    info_plist={
        "NSPrincipalClass": "NSApplication",
        "NSAppleScriptEnabed": False,
        "NSHighResolutionCapable": True,
        "NSMicrophoneUsageDescription": "Allow Audiotext to record audio from your microphone to generate transcriptions.",
    }
)

这解决了问题。但是,我现在遇到了另一个问题,因为

Audiotext.app
文件无法打开,而
Audiotext
目录中的
dist/Audiotext
可执行文件却可以打开。但与本主题无关,所以我将其标记为答案。

© www.soinside.com 2019 - 2024. All rights reserved.