如何在 OS X 上安装 Python 扩展时解决“Missing MacOSX10.4u.sdk”错误

114 阅读2分钟

在 OS X (10.6.4) 上安装 Python 扩展时,可能会遇到“Missing MacOSX10.4u.sdk”错误。例如,在编译 Cython 时,可能会出现如下错误信息:

huake_00152_.jpg

In file included from /usr/include/architecture/i386/math.h:626,
                 from /usr/include/math.h:28,
                 from /Library/Frameworks/Python.framework/Versions/2.6/include/python2.6/pyport.h:235,
                 from /Library/Frameworks/Python.framework/Versions/2.6/include/python2.6/Python.h:58,
                 from /tmp/easy_install-Sgn5ep/Cython-0.12.1/Cython/Plex/Scanners.c:4:
/usr/include/AvailabilityMacros.h:108:14: warning: #warning Building for Intel with Mac OS X Deployment Target < 10.4 is invalid.
Compiling with an SDK that doesn't seem to exist: /Developer/SDKs/MacOSX10.4u.sdk
Please check your Xcode installation
ld: library not found for -lbundle1.o
ld: library not found for collect2: -lbundle1.o
collect2: ld returned 1 exit status

类似的错误信息也可能在安装 lxml 时出现。

2、解决方案

出现此错误的原因是,缺少一个旧的 OS X SDK,即 MacOSX10.4u.sdk。要解决此问题,可以尝试以下解决方法:

  1. 安装 Xcode:

    • 打开 App Store,搜索 Xcode,并安装最新版本的 Xcode。
    • 安装 Xcode 后,请确保已安装 Command Line Tools。可以在 Xcode 中通过“Xcode”>“Preferences”>“Locations”来检查和安装 Command Line Tools。
  2. 安装 OS X SDK:

    • 打开 Xcode,然后选择“File”>“New”>“Project”。
    • 在“New Project”对话框中,选择“Command-Line Tool”模板,然后单击“Next”。
    • 在“Project Details”页面中,输入项目名称和位置,然后单击“Create”。
    • 在 Xcode 项目中,选择“Product”>“Edit Scheme”。
    • 在“Edit Scheme”对话框中,选择“Build”>“Build Configuration”,然后选择“Release”。
    • 在“Build Configuration”部分中,将“Deployment Target”设置为“Mac OS X 10.4”。
    • 单击“Close”按钮关闭“Edit Scheme”对话框。
    • 选择“Product”>“Build”。
    • 等待 Xcode 构建项目。
  3. 将 SDK 路径添加到环境变量:

    • 打开终端,并运行以下命令:
    echo $SDKROOT
    
    • 应该会看到类似以下的输出:
    /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.4u.sdk
    
    • 将此路径添加到 $PATH 环境变量中。可以在 .bashrc.zshrc 文件中添加以下行:
    export PATH="$PATH:/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.4u.sdk"
    
    • 重新加载 .bashrc.zshrc 文件,或者重新启动终端。
  4. 重新安装 Python 扩展:

    • 现在可以重新安装 Python 扩展了。例如,要安装 Cython,可以运行以下命令:
    pip install Cython
    
    • 要安装 lxml,可以运行以下命令:
    pip install lxml
    
  5. 验证安装:

    • 安装完成后,可以运行以下命令来验证是否已成功安装 Python 扩展:
    python -c "import cython"
    python -c "import lxml"
    

如果以上命令没有报错,则表明 Python 扩展已成功安装。