最近重做了一下Mac 系统,Python开发环境需要重新安装。pyenv 很久没有碰,记录一下踩坑记录。
安装pyenv
方式1(推荐):一键安装
curl -L https://github.com/pyenv/pyenv-installer/raw/master/bin/pyenv-installer | bash
适用于各平台
方式2:使用git来安装
git clone https://github.com/pyenv/pyenv.git ~/.pyenv
在bashrc 或者 zshrc中添加如下脚本:
export PYENV_ROOT="$HOME/.pyenv"
[[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"
pyenv 国内镜像添加Python版本:
export v=3.12.4; wget https://mirrors.huaweicloud.com/python/$v/Python-$v.tar.xz -P ~/.pyenv/cache/; pyenv install $v
目前华为的镜像相对稳定快速,淘宝的当下无法使用,也可以尝试其他Python镜像,留意不是pip镜像。
安装Python版本的报错
export v=3.12.4; wget https://mirrors.huaweicloud.com/python/$v/Python-$v.tar.xz -P ~/.pyenv/cache/; pyenv install $v
--2024-06-12 10:20:23-- https://mirrors.huaweicloud.com/python/3.12.4/Python-3.12.4.tar.xz
Resolving mirrors.huaweicloud.com (mirrors.huaweicloud.com)... 124.70.125.153, 124.70.125.167
Connecting to mirrors.huaweicloud.com (mirrors.huaweicloud.com)|124.70.125.153|:443... connected.
HTTP request sent, awaiting response... 200
Length: 20659356 (20M) [application/octet-stream]
Saving to: ‘/root/.pyenv/cache/Python-3.12.4.tar.xz’
Python-3.12.4.tar.xz 100%[============================>] 19.70M 4.91MB/s in 4.0s
2024-06-12 10:20:28 (4.91 MB/s) - ‘/root/.pyenv/cache/Python-3.12.4.tar.xz’ saved [20659356/20659356]
Installing Python-3.12.4...
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/root/.pyenv/versions/3.12.4/lib/python3.12/bz2.py", line 17, in <module>
from _bz2 import BZ2Compressor, BZ2Decompressor
ModuleNotFoundError: No module named '_bz2'
WARNING: The Python bz2 extension was not compiled. Missing the bzip2 lib?
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/root/.pyenv/versions/3.12.4/lib/python3.12/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>
File "/root/.pyenv/versions/3.12.4/lib/python3.12/ctypes/__init__.py", line 8, in <module>
from _ctypes import Union, Structure, Array
ModuleNotFoundError: No module named '_ctypes'
WARNING: The Python ctypes extension was not compiled. Missing the libffi 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 "/root/.pyenv/versions/3.12.4/lib/python3.12/sqlite3/__init__.py", line 57, in <module>
from sqlite3.dbapi2 import *
File "/root/.pyenv/versions/3.12.4/lib/python3.12/sqlite3/dbapi2.py", line 27, in <module>
from _sqlite3 import *
ModuleNotFoundError: No module named '_sqlite3'
WARNING: The Python sqlite3 extension was not compiled. Missing the SQLite3 lib?
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/root/.pyenv/versions/3.12.4/lib/python3.12/tkinter/__init__.py", line 38, in <module>
import _tkinter # If this fails your Python may not be configured for Tk
^^^^^^^^^^^^^^^
ModuleNotFoundError: No module named '_tkinter'
WARNING: The Python tkinter extension was not compiled and GUI subsystem has been detected. Missing the Tk toolkit?
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/root/.pyenv/versions/3.12.4/lib/python3.12/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.12.4 to /root/.pyenv/versions/3.12.4
这种情况是因缺少依赖库:
sudo apt install -y make build-essential libssl-dev zlib1g-dev libbz2-dev libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev tk-dev libffi-dev liblzma-dev libreadline-dev
其他平台根据情况修改