You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Mac系统cx_freeze打包Python可执行文件运行报错解决方案咨询

Fixing "dyld: Library not loaded" Error with cx_Freeze on macOS

This is a super common issue with cx_Freeze on macOS—your bundled executable is still hardcoded to reference your local Python 3.6 framework path, which doesn’t exist on your users’ machines. The good news is you absolutely can fix this by tweaking your setup script and repackaging. Here’s how:

1. Update setup.py to bundle the Python framework

Modify your build config to explicitly include the Python framework in your packaged files. This ensures the framework gets distributed right alongside your executable, so users don’t need Python installed locally.

Here’s a tailored example setup.py for macOS:

import sys
import os
from cx_Freeze import setup, Executable

# Set base type (Console for terminal apps, GUI for desktop apps)
base = None
if sys.platform == "darwin":
    base = "Console"  # Swap to "GUI" if your app has a desktop interface

# Define your main executable
executables = [Executable("your_main_script.py", base=base)]

# Core build configuration
build_options = {
    "packages": [],  # Add any third-party packages your script depends on here
    "excludes": [],
    "optimize": 2,
    "zip_include_packages": "*",
    "zip_exclude_packages": ""
}

# Add the Python framework to included files (macOS-only step)
if sys.platform == "darwin":
    python_framework_path = "/Library/Frameworks/Python.framework/Versions/3.6"
    build_options["include_files"].append(
        (python_framework_path, "Python.framework/Versions/3.6")
    )

setup(
    name="YourAppName",
    version="1.0",
    description="Your app description here",
    options={"build_exe": build_options},
    executables=executables
)

Instead of just building a standalone executable, use cx_Freeze’s bdist_mac command to create a standard macOS .app package. This tool handles dependency path adjustments and framework bundling automatically, which is way more reliable for distribution:

python setup.py bdist_mac

The resulting .app bundle will contain all necessary files (including the Python framework) and should run seamlessly on other macOS machines.

3. Manually fix dependency paths (if needed)

If you still hit issues after repackaging, you can manually update the executable’s library references using install_name_tool (a built-in macOS tool). Navigate to your build directory’s executable and run:

install_name_tool -change /Library/Frameworks/Python.framework/Versions/3.6/Python @executable_path/../Python.framework/Versions/3.6/Python YourExecutableName

This rewrites the dependency path to point to the bundled framework relative to the executable.

Quick Test Tip

To confirm your fix works, test the bundled app on a machine without Python 3.6 installed, or temporarily rename your local /Library/Frameworks/Python.framework/Versions/3.6 folder to simulate a user’s environment.

内容的提问来源于stack exchange,提问作者kishan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 08:12:02