-
问题描述 (The Problem) 如果你在 macOS 上使用 poetry install 命令时,遇到了类似下面的错误(通常是某些依赖包编译失败),那么这个帖子可能会对你有所帮助。 解决方案:使用 Docker 构建一致的开发环境 (The Solution: Docker for a Consistent Environment) A robust solution is to use Docker to build and manage your development environment. This creates a consistent and clean Linux-based environment, bypassing macOS-specific issues. 解决方案已更新。 |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
请问你的macOS版本和所使用的Poetry版本是什么呀? What is your macOS version and the Poetry version you are using? |
Beta Was this translation helpful? Give feedback.
-
[root cause]Apple Silicon Mac 上的 Python 终极陷阱:Rosetta 2 架构陷阱 在 Apple Silicon (M1/M2/M3) Mac 上,当你使用 Poetry (或 pip) 安装 Python 包时,反复遇到“找不到兼容版本”的错误。这个问题的根源通常分为两个层面:最底层的是 Homebrew 自身的架构问题,它直接导致了上一层的 Python 解释器架构问题。 陷阱零:Homebrew 自身的架构问题 (Trap Zero: The Homebrew Architecture Problem) 这是最隐蔽、最根本的陷阱。如果你是在 Apple Silicon Mac 上从 Intel Mac 迁移过来的,或者早期安装了 Homebrew,那么你的 Homebrew 很可能就是 Intel (x86_64) 版本。 问题详解 (Detailed Explanation) 错误的安装位置:一个为 Intel 芯片安装的 Homebrew,它会把自己和它管理的所有软件都安装在 /usr/local 目录下。当它在 Apple Silicon Mac 上运行时,整个 Homebrew 和它安装的软件(包括 Python)都会通过 Rosetta 2 转译层运行。 连锁反应:这个 Intel 版的 Homebrew 在安装 Python 时,只会下载和安装 Intel (x86_64) 版本的 Python。这直接导致了下一层陷阱的发生。 如何诊断 (How to Diagnose) 检查 brew 命令的路径是唯一的诊断方法。 which brew 错误情况 (Intel版): 输出是 /usr/local/bin/brew。 正确情况 (原生ARM版): 输出应该是 /opt/homebrew/bin/brew。 如何修正 (How to Fix) 你必须安装并使用原生的 ARM 版 Homebrew。 安装原生 ARM 版 Homebrew:运行官方安装脚本,它会自动安装到正确的 /opt/homebrew 目录。 /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" 修正你的 Shell 环境:确保你的终端优先使用新的 Homebrew。最可靠的方法是运行以下命令,它会自动配置你的 ~/.zshrc (或其他 Shell 配置文件)。 echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.zshrc 重新加载配置:完全关闭终端再重新打开,或者运行 source ~/.zshrc。验证 which brew 现在是否指向 /opt/homebrew/bin/brew。 陷阱一:Python 解释器的架构问题 (Trap One: The Python Interpreter Architecture Problem) 这个陷阱是“陷阱零”的直接后果。 问题详解 (Detailed Explanation) Rosetta 2 的“幻觉”:由 Intel 版 Homebrew 安装的 Python 解释器,它以为自己运行在一台 Intel 电脑上。 错误的架构标签:当 Poetry 向这个 Python 解释器查询其兼容架构时,它只会报告 x86_64,完全不知道 arm64 的存在。 包安装失败:Poetry 拿着错误的 x86_64 标签去寻找包,自然找不到为 Apple Silicon 准备的原生 arm64 包,导致安装失败。 如何诊断 (How to Diagnose) 使用 file 命令检查二进制文件类型: 确保你已在项目目录并激活了环境,或使用 poetry env infofile $(poetry env info --path)/bin/python 错误情况 (Intel版): 输出会包含 ... executable x86_64 正确情况 (原生ARM版): 输出应该包含 ... executable arm64 让 Python 自己报告架构: poetry run python -c "import platform; print(platform.machine())" 错误情况 (Intel版): 输出是 x86_64 正确情况 (原生ARM版): 输出应该是 arm64 如何修正 (How to Fix) 在确保你的 Homebrew 是原生 ARM 版本后,执行以下步骤。 安装原生 Python:使用你新的、正确的 brew 来安装 Python。 brew install python@3.12 为项目指定正确的 Python:在你的项目目录下,运行 poetry env use 并传入原生 Python 的完整路径。 poetry env use /opt/homebrew/bin/python3.12 重建虚拟环境:删除旧的、基于错误架构的虚拟环境,然后重新安装。 rm -rf $(poetry env info --path) 完成这些步骤后,你的项目就会在一个纯净、正确的原生 ARM 环境中运行。 English Summary On an Apple Silicon (M1/M2/M3) Mac, you repeatedly encounter "Unable to find installation candidates" errors when installing Python packages. This issue typically has two layers: the deepest is the architecture problem of Homebrew itself, which directly causes the Python interpreter architecture problem. Trap Zero: The Homebrew Architecture Problem This is the most fundamental and hidden trap. If you migrated from an Intel Mac or installed Homebrew in the early days of Apple Silicon, your Homebrew installation is likely the Intel (x86_64) version. Detailed Explanation Incorrect Installation Path: A Homebrew installed for Intel chips places itself and all its managed software in the /usr/local directory. When running on an Apple Silicon Mac, this entire Homebrew setup operates through the Rosetta 2 translation layer. Chain Reaction: This Intel-based Homebrew, when installing Python, will only download and install the Intel (x86_64) version of Python, which directly leads to the next trap. How to Diagnose Checking the path of the brew command is the definitive diagnostic method. which brew Incorrect (Intel version): The output is /usr/local/bin/brew. Correct (Native ARM version): The output should be /opt/homebrew/bin/brew. How to Fix You must install and use the native ARM version of Homebrew. Install Native ARM Homebrew: Run the official installation script, which automatically installs it to the correct /opt/homebrew directory. /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" Fix Your Shell Environment: Ensure your terminal uses the new Homebrew by default. The most reliable method is to run the following command to configure your ~/.zshrc (or other shell config file). echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.zshrc Reload Configuration: Close and reopen your terminal completely, or run source ~/.zshrc. Verify that which brew now points to /opt/homebrew/bin/brew. Trap One: The Python Interpreter Architecture Problem This trap is a direct consequence of "Trap Zero." Detailed Explanation The Rosetta 2 "Illusion": The Python interpreter installed by the Intel-based Homebrew thinks it's running on an Intel machine. Incorrect Architecture Tags: When Poetry asks this Python for its compatible architectures, it only reports x86_64, completely unaware of arm64. Package Installation Failure: Poetry takes these incorrect x86_64 tags to find packages and naturally fails to find the native arm64 packages intended for Apple Silicon. How to Diagnose Check the binary file type with file: file $(poetry env info --path)/bin/python Incorrect (Intel version): Output contains ... executable x86_64 Correct (Native ARM version): Output should contain ... executable arm64 Have Python report its own architecture: poetry run python -c "import platform; print(platform.machine())" Incorrect (Intel version): Output is x86_64 Correct (Native ARM version): Output should be arm64 How to Fix After ensuring your Homebrew is the native ARM version, follow these steps. Install Native Python: Use your new, correct brew to install Python. brew install python@3.12 Point Your Project to the Correct Python: In your project directory, run poetry env use with the full path to the native Python. poetry env use /opt/homebrew/bin/python3.12 Recreate the Virtual Environment: Delete the old, incorrectly-architected venv and reinstall. rm -rf $(poetry env info --path) After completing these steps, your project will run in a clean, correct, and native ARM environment. |
Beta Was this translation helpful? Give feedback.
-
感谢您详细的回复。这将大大帮助社区中的许多人。 Thank you for your detailed response. It will greatly help many people in the community. |
Beta Was this translation helpful? Give feedback.
[root cause]
Apple Silicon Mac 上的 Python 终极陷阱:Rosetta 2 架构陷阱
The Ultimate Python Pitfall on Apple Silicon Macs: The Rosetta 2 Architecture Trap
中文总结
核心问题 (The Core Problem)
在 Apple Silicon (M1/M2/M3) Mac 上,当你使用 Poetry (或 pip) 安装 Python 包时,反复遇到“找不到兼容版本”的错误。这个问题的根源通常分为两个层面:最底层的是 Homebrew 自身的架构问题,它直接导致了上一层的 Python 解释器架构问题。
陷阱零:Homebrew 自身的架构问题 (Trap Zero: The Homebrew Architecture Problem)
这是最隐蔽、最根本的陷阱。如果你是在 Apple Silicon Mac 上从 Intel Mac 迁移过来的,或者早期安装了 Homebrew,那么你的 Homebrew 很可能就是 Intel (x86_64) 版本。
问题详解 (Detailed Explanation)
错误的安装位置:一个为 Intel 芯片安装的 Homebrew,它会把自己和它管理的所有软件都安装在 /usr/local 目录下。当它在 Apple Silicon Mac 上运行时,整个 Homebrew 和它安装的软件(包括 Python)都会通过 Rosetta 2 转译层运行。
连锁反应:这个 Intel 版的 Home…