Linux通过pyenv安装python,解决缺少库报错
一、安装pyenv
-
下载源码(通过git下载源码)
将源码下载到用户目录下方的隐藏文件夹./pyenv
git clone https://github.com/yyuu/pyenv.git ~/.pyenv
-
安装目录结构(/home/用户名/.pyenv)(cache为后加的目录)
-
添加用户环境变量
-
打开用户目录的隐藏Shell配置文件(/home/用户名/.bashrc)
-
写入以下配置
# 下面是pyenv的设置
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/libexec:$PATH"
eval "$(pyenv init --path)"
- 验证
#重启shell
source ~/.bashrc
#验证输入
pyenv -v
#输出
pyenv 2.3.29
至此pyenv安装完毕。
二、安装python3.10.13
- 方法一:直接运行以下代码。(速度较慢)
pyenv install 3.10.13
- 方法二:从python官网下载可能有点慢。直接从镜像或者其他地方将下载好的Python-3.10.13.tar.xz。放置到pyenv根目录下新建的cache文件内即可。(没有cache目录,新建即可)然后再运行pyenv install 3.10.13,就可以自动跳过下载,直接安装。
用户名@用户名:~/.pyenv/cache$ pwd
/home/用户名/.pyenv/cache
用户名@用户名:~/.pyenv/cache$ ls
Python-3.10.13.tar.xz
用户名@用户名:~/.pyenv/cache$ pyenv install
Installing Python-3.10.13...
三、安装报WARNING,缺少_bz2、_sqlite3等库
ModuleNotFoundError: No module named '_bz2'
WARNING: The Python bz2 extension was not compiled. Missing the bzip2 lib?
ModuleNotFoundError: No module named '_curses'
WARNING: The Python curses extension was not compiled. Missing the ncurses lib?
ModuleNotFoundError: No module named 'readline'
WARNING: The Python readline extension was not compiled. Missing the GNU readline lib?
ModuleNotFoundError: No module named '_sqlite3'
WARNING: The Python sqlite3 extension was not compiled. Missing the SQLite3 lib?
ModuleNotFoundError: No module named '_tkinter'
WARNING: The Python tkinter extension was not compiled and GUI subsystem has been detected. Missing the Tk toolkit?
ModuleNotFoundError: No module named '_lzma'
WARNING: The Python lzma extension was not compiled. Missing the lzma lib?
原因:Linux缺少对应的库文件,进行对应的安装就行。
sudo apt-get install libbz2-dev
sudo apt-get install libncurses5-dev
sudo apt-get install libreadline-dev tk-dev
sudo apt-get install libsqlite3-dev
sudo apt-get install python3-tk tk-dev
sudo apt-get install liblzma-dev
//或者一行
sudo apt-get install libbz2-dev libncurses5-dev libreadline-dev tk-dev libsqlite3-dev python3-tk tk-dev liblzma-dev
然后再将python3.10.13卸载,重装
用户名@用户名:~$ pyenv uninstall 3.10.13
pyenv: remove /home/用户名/.pyenv/versions/3.10.13? [y|N] y
pyenv: 3.10.13 uninstalled
用户名@用户名:~$ pyenv install 3.10.13
Installing Python-3.10.13...
Installed Python-3.10.13 to /home/用户名/.pyenv/versions/3.10.13