MacBook M3 Pro上pyenv无法安装Python版本的问题求助
MacBook M3 Pro 下 pyenv 安装 Python 版本失败问题
问题详情
- 设备:MacBook M3 Pro,已从旧款Intel Pro迁移所有内容
- 异常表现:安装pyenv后,尝试安装任意Python版本(3.9.x、3.10.x、3.11.x等)均出现构建错误,提示缺失
_curses、readline、_ssl等模块 - 核心诱因:Apple M系列arm64架构与原x86架构Brew的兼容性冲突;此前因tensorflow导致Jupyter Notebook内核崩溃,遂排查此问题
报错日志
python-build: use openssl@3 from homebrew python-build: use readline from homebrew Downloading Python-3.11.9.tar.xz... -> https://www.python.org/ftp/python/3.11.9/Python-3.11.9.tar.xz Installing Python-3.11.9... python-build: use tcl-tk from homebrew python-build: use readline from homebrew python-build: use ncurses from homebrew python-build: use zlib from xcode sdk Traceback (most recent call last): File "<string>", line 1, in <module> File "/Users/ashtonsperry/.pyenv/versions/3.11.9/lib/python3.11/curses/__init__.py", line 13, in <module> from _curses import * ModuleNotFoundError: No module named '_curses' WARNING: The Python curses extension was not compiled. Missing the ncurses lib? Traceback (most recent call last): File "<string>", line 1, in <module> ModuleNotFoundError: No module named 'readline' WARNING: The Python readline extension was not compiled. Missing the GNU readline lib? Traceback (most recent call last): File "<string>", line 1, in <module> File "/Users/ashtonsperry/.pyenv/versions/3.11.9/lib/python3.11/ssl.py", line 100, in <module> import _ssl # if we can't import it, let the error propagate ^^^^^^^^^^^ ModuleNotFoundError: No module named '_ssl' ERROR: The Python ssl extension was not compiled. Missing the OpenSSL lib? Please consult to the Wiki page to fix the problem. https://github.com/pyenv/pyenv/wiki/Common-build-problems BUILD FAILED (OS X 14.5 using python-build 20180424) Inspect or clean up the working tree at /var/folders/p3/7nvxx8mx4x19125mj6fnjj0h0000gn/T/python-build.20240708161245.10785 Results logged to /var/folders/p3/7nvxx8mx4x19125mj6fnjj0h0000gn/T/python-build.20240708161245.10785.log Last 10 log lines: DYLD_LIBRARY_PATH=/var/folders/p3/7nvxx8mx4x19125mj6fnjj0h0000gn/T/python-build.20240708161245.10785/Python-3.11.9 ./python.exe -E -m ensurepip \ $ensurepip --root=/ ; \ fi Looking in links: /var/folders/p3/7nvxx8mx4x19125mj6fnjj0h0000gn/T/tmp8_w0_bq0 Processing /var/folders/p3/7nvxx8mx4x19125mj6fnjj0h0000gn/T/tmp8_w0_bq0/setuptools-65.5.0-py3-none-any.whl Processing /var/folders/p3/7nvxx8mx4x19125mj6fnjj0h0000gn/T/tmp8_w0_bq0/pip-24.0-py3-none-any.whl Installing collected packages: setuptools, pip WARNING: The scripts pip3 and pip3.11 are installed in '/Users/ashtonsperry/.pyenv/versions/3.11.9/bin' which is not on PATH. Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location. Successfully installed pip-24.0 setuptools-65.5.0
解决步骤
1. 替换x86架构Brew为arm64版本
- 卸载原有x86版本Brew及依赖:
/usr/local/bin/brew uninstall --force --ignore-dependencies $(/usr/local/bin/brew list) rm -rf /usr/local/Homebrew rm -rf /usr/local/Caskroom rm -rf /usr/local/bin/brew - 安装arm64版本Brew:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" - 配置环境变量确保arm64 Brew优先级:
在~/.zshrc或~/.bash_profile中添加:
生效配置:export PATH="/opt/homebrew/bin:$PATH"source ~/.zshrc
2. 安装Python编译所需依赖
brew install openssl readline ncurses zlib tcl-tk
3. 设置pyenv编译环境变量
在~/.zshrc或~/.bash_profile中添加:
export LDFLAGS="-L/opt/homebrew/opt/openssl/lib -L/opt/homebrew/opt/readline/lib -L/opt/homebrew/opt/ncurses/lib -L/opt/homebrew/opt/zlib/lib" export CPPFLAGS="-I/opt/homebrew/opt/openssl/include -I/opt/homebrew/opt/readline/include -I/opt/homebrew/opt/ncurses/include -I/opt/homebrew/opt/zlib/include" export PKG_CONFIG_PATH="/opt/homebrew/opt/openssl/lib/pkgconfig"
生效配置:
source ~/.zshrc
4. 清理残留并重新安装Python
- 卸载失败的Python版本:
pyenv uninstall 3.11.9 - 清理临时编译文件:
rm -rf /var/folders/p3/7nvxx8mx4x19125mj6fnjj0h0000gn/T/python-build.* - 重新安装目标Python版本:
pyenv install 3.11.9
内容的提问来源于stack exchange,提问作者Ashton T. Sperry
相关产品推荐
相关产品推荐

