自动摘要
正在生成中……
pyenv install 3.11.9时报错以下:
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](https://www.python.org/ftp/python/3.11.9/Python-3.11.9.tar.xz)
Installing Python-3.11.9...
python-build: use readline from homebrew
python-build: use zlib from xcode sdk
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/Users/falcon/.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](https://github.com/pyenv/pyenv/wiki/Common-build-problems)
BUILD FAILED (OS X 15.2 using python-build 20180424)
这个错误是 pyenv 安装 Python 3.11.9 时 无法正确编译 ssl
模块,也就是:
ModuleNotFoundError: No module named '_ssl'
这通常表示编译时没正确链接到 openssl
库。尽管 pyenv 提示 “use openssl@3 from homebrew”,但默认可能没有正确传入编译参数。
✅ 解决方案步骤
1️⃣ 安装依赖(确保 openssl@3
可用)
brew install openssl@3 readline zlib xz
有时 xz
缺失也会导致 tar 包解析失败。
2️⃣ 设置环境变量(最关键)
在安装前先设置这些变量,让 pyenv 编译器正确找到 openssl:
export LDFLAGS="-L/opt/homebrew/opt/openssl@3/lib -L/opt/homebrew/opt/readline/lib -L/opt/homebrew/opt/zlib/lib"
export CPPFLAGS="-I/opt/homebrew/opt/openssl@3/include -I/opt/homebrew/opt/readline/include -I/opt/homebrew/opt/zlib/include"
export PKG_CONFIG_PATH="/opt/homebrew/opt/openssl@3/lib/pkgconfig:/opt/homebrew/opt/readline/lib/pkgconfig"
你可以加到 ~/.zshrc
或运行时手动设置。
3️⃣ 重新安装 Python 3.11.9
现在重新尝试:
pyenv install 3.11.9
这次应该能成功编译带有 _ssl
的 ssl
模块。
✅ 可选:全局配置 pyenv 的 build 环境(推荐)
如果你经常编译 Python,可以配置 .zshrc
永久设置这些环境变量:
# ~/.zshrc
export LDFLAGS="-L/opt/homebrew/opt/openssl@3/lib -L/opt/homebrew/opt/readline/lib -L/opt/homebrew/opt/zlib/lib"
export CPPFLAGS="-I/opt/homebrew/opt/openssl@3/include -I/opt/homebrew/opt/readline/include -I/opt/homebrew/opt/zlib/include"
export PKG_CONFIG_PATH="/opt/homebrew/opt/openssl@3/lib/pkgconfig:/opt/homebrew/opt/readline/lib/pkgconfig"
然后:
source ~/.zshrc