环境
我的电脑环境:苹果Mac mini M4 钙中钙版本,osx15.3.1
使用homebrew安装pyenv,版本为2.5.3
➜ ~ pyenv -v
pyenv 2.5.3
错误信息
在使用pyenv安装Python时,会出现这个错误
➜ ~ pyenv install 3.13.2
python-build: use openssl@3 from homebrew
python-build: use readline from homebrew
Downloading Python-3.13.2.tar.xz...
-> https://www.python.org/ftp/python/3.13.2/Python-3.13.2.tar.xz
Installing Python-3.13.2...
python-build: use readline from homebrew
python-build: use zlib from xcode sdk
Traceback (most recent call last):
File "<string>", line 1, in <module>
import lzma
File "/Users/***/.pyenv/versions/3.13.2/lib/python3.13/lzma.py", line 27, in <module>
from _lzma import *
ModuleNotFoundError: No module named '_lzma'
WARNING: The Python lzma extension was not compiled. Missing the lzma lib?
Installed Python-3.13.2 to /Users/***/.pyenv/versions/3.13.2
这是由于缺少_lzma模块导致的,而_lzma模块是用于提供对LZMA压缩算法的支持的Python扩展模块。
解决办法
_lzma模块依赖于XZ库,因此需要先安装XZ库。在终端中使用以下命令安装XZ库:
➜ ~ brew install xz
==> Downloading https://formulae.brew.sh/api/formula.jws.json
==> Downloading https://formulae.brew.sh/api/cask.jws.json
==> Downloading https://ghcr.io/v2/homebrew/core/xz/manifests/5.6.4
############################################################################## 100.0%
==> Fetching xz
==> Downloading https://ghcr.io/v2/homebrew/core/xz/blobs/sha256:b49f3559f9425b0a8c8d
############################################################################## 100.0%
==> Pouring xz--5.6.4.arm64_sequoia.bottle.tar.gz
🍺 /opt/homebrew/Cellar/xz/5.6.4: 96 files, 2.0MB
==> Running `brew cleanup xz`...
Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).
安装xz后,需要卸载刚刚没有正确安装的python
➜ ~ pyenv uninstall 3.13.2
pyenv: remove /Users/***/.pyenv/versions/3.13.2? (y/N) y
pyenv: 3.13.2 uninstalled
卸载成功后,在重新安装即可
➜ ~ pyenv install 3.13.2
python-build: use openssl@3 from homebrew
python-build: use readline from homebrew
Downloading Python-3.13.2.tar.xz...
-> https://www.python.org/ftp/python/3.13.2/Python-3.13.2.tar.xz
Installing Python-3.13.2...
python-build: use readline from homebrew
python-build: use zlib from xcode sdk
Installed Python-3.13.2 to /Users/***/.pyenv/versions/3.13.2
问题解决
➜ ~ pyenv global 3.13.2
➜ ~ python -V
Python 3.13.2
➜ ~ pip -V
pip 24.3.1 from /Users/***/.pyenv/versions/3.13.2/lib/python3.13/site-packages/pip (python 3.13)