Ubuntu WSL中使用pyenv安装Python时出现_ssl模块缺失错误
操作流程
- 安装Homebrew:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" - 更新brew并安装pyenv:
brew update brew install pyenv - 执行pyenv安装指定Python版本:
pyenv install 3.10.13
报错信息
Downloading Python-3.10.13.tar.xz...
-> https://www.python.org/ftp/python/3.10.13/Python-3.10.13.tar.xz
Installing Python-3.10.13...
Traceback (most recent call last):
File "", line 1, in
File "/home/adam/.pyenv/versions/3.10.13/lib/python3.10/ssl.py", line 99, in
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.
BUILD FAILED (Ubuntu 20.04 using python-build 2.3.35-5-g2d850751)
Inspect or clean up the working tree at /tmp/python-build.20231227145009.23362
Results logged to /tmp/python-build.20231227145009.23362.logLast 10 log lines:
LD_LIBRARY_PATH=/tmp/python-build.20231227145009.23362/Python-3.10.13 ./python -E -m ensurepip
$ensurepip --root=/ ;
fi
Looking in links: /tmp/tmprqhmro5y
Processing /tmp/tmprqhmro5y/setuptools-65.5.0-py3-none-any.whl
Processing /tmp/tmprqhmro5y/pip-23.0.1-py3-none-any.whl
Installing collected packages: setuptools, pip
WARNING: The scripts pip3 and pip3.10 are installed in '/home/adam/.pyenv/versions/3.10.13/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-23.0.1 setuptools-65.5.0
已尝试sudo apt install libssl-dev但问题未解决,请问错误原因是什么?
错误原因
- OpenSSL路径冲突:通过Homebrew安装的pyenv会优先调用Homebrew自带的OpenSSL库,而Ubuntu系统的libssl-dev安装在系统路径下,导致Python编译时无法正确找到并链接系统的SSL库。
- 依赖不完整:仅安装libssl-dev不够,Python编译SSL模块还需要
zlib1g-dev、libffi-dev、build-essential等基础编译依赖,缺失这些会导致SSL模块编译失败。 - 版本兼容性问题:Ubuntu 20.04默认的OpenSSL 1.1.1版本,与Python 3.10.13编译时的预期可能存在细微兼容性差异,或者编译脚本未正确识别该版本。
- 编译缓存残留:之前编译失败后,
/tmp/python-build.*目录下的残留文件会干扰后续编译,即使安装了依赖也无法重新编译SSL模块。
内容的提问来源于stack exchange,提问作者Goku

