更新程序包并安装变异依赖环境
sudo apt update && sudo apt upgrade
sudo apt install wget build-essential libncursesw5-dev libssl-dev libsqlite3-dev tk-dev libgdbm-dev libc6-dev libbz2-dev libffi-dev zlib1g-dev
开始正式安装
1.下载Python源码包
可以前往Python官网获取最新源码
cd ~
wget https://www.python.org/ftp/python/3.13.0/Python-3.13.0.tgz
2.解压Python源码
将下载好的源码包进行解压,默认放在当前文件夹下的压缩包同名文件夹内
tar xzf Python-3.13.0.tgz
3.编译Python源码
进入解压后的文件夹内,进行选项配置
cd Python-3.13.0
./configure --enable-optimizations
#--enable-optimizations为优化性能选项,其余类似的还有 --prefix=PATH 指定安装目录……,可根据需要进行选择。
#默认安装路径为 /usr/local/bin
4.安装Python 3.13
make altinstall
#altinstall用于防止编译器覆盖默认Python版本
5.验证安装
root@debian:~ # python3.10
Python 3.10.0 (default, Dec 5 2021, 22:46:09) [GCC 10.2.1 20210110] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
至此,已完成Python3.13
的安装
接下来可以根据需要选择是否需要更改默认Python
为Python3.13
切换Python版本
可以使用以下两个命令 whereis
或which
确定已安装python
的版本和路径:
#whereis:适用于查看目前已安装的所有Python版本及路径
root@debian:~ # whereis python
python: /usr/bin/python3.9 /usr/lib/python2.7 /usr/lib/python3.9 /etc/python3.9
/usr/local/bin/python3.13 /usr/local/bin/python3.13-config /usr/local/lib/python3.13
/usr/local/lib/python3.9
#which:适用于查看具体某个python版本的安装路径
root@debian:~ # which python3.13
/usr/local/bin/python3.13
为单个用户切换Python版本
只需要在该用户home
目录下的 .bashrc
文件下新增 Alias
即可
alias python='/usr/local/bin/python3.10'
#python具体版本和路径可根据个人需要确定
修改完毕后,使用source ~/.bashrc
命令,重新加载 .bashrc
文件,使其生效
系统级切换Python版本
使用update-alternatives --list python
命令,为整个系统更改Python
版本
1.列出所有可用Python替代版本
root@debian:~ # update-alternatives --list python
/usr/lib/python2.7
/usr/lib/python3.9
/usr/local/bin/python3.13
2.添加替代版本列表
如果运行后出现错误信息:update-alternatives: error:no alternatives for python
则为没有更新替代版本列表,使用以下命令添加:
#注意:update-alternatives --install <link> <name> <path> <priority>
#1.<link>一般情况下,直接使用 /usr/bin/python 即可
#2.<name>即为需要更换的python
#3.<path>为需要添加的python版本的安装路径,可以在上文中确定
#4.<priorit>为优先级。数字越大,优先级越高
root@debian:~ # update-alternatives --install /usr/bin/python python /usr/lib/python2.7 1
#update-alternatives: using /usr/lib/python2.7 to provide /usr/bin/python (python) in auto mode
root@debian:~ # update-alternatives --install /usr/bin/python python /usr/lib/python3.9 2
#update-alternatives: using /usr/lib/python3.9 to provide /usr/bin/python (python) in auto mode
root@debian:~ # update-alternatives --install /usr/bin/python python /usr/local/bin/python3.13 3
update-alternatives: using /usr/local/bin/python3.13 to provide /usr/bin/python (python) in auto mode
至此,系统已默认Python
版本为3.13
,验证如下:
root@debian:~ # python
Python 3.13.0 (main, Oct 16 2024, 21:16:02) [GCC 10.2.1 20210110] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
3.进行版本切换
使用update-alternatives --config python
命令即可
root@raspberrypi:~ # update-alternatives --config python
There are 3 choices for the alternative python (providing /usr/bin/python).
Selection Path Priority Status
------------------------------------------------------------
* 0 /usr/local/bin/python3.13 3 auto mode
1 /usr/lib/python2.7 1 manual mode
2 /usr/lib/python3.9 2 manual mode
3 /usr/local/bin/python3.13 3 manual mode
Press <enter> to keep the current choice[*], or type selection number: 2
update-alternatives: using /usr/lib/python3.9 to provide /usr/bin/python (python) in manual mode
root@raspberrypi:~ # python
Python 3.9.2 (main, Oct 16 2024, 21:18:34)
[GCC 10.2.1 20210110] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
扩展:创建python虚拟环境
python3 -m venv myenv
#创建虚拟环境
source myenv/bin/activate
#进入虚拟环境