ubuntu 18.04 自带 python 基础版本:python 2.7.17
、python 3.6.9
一、直接安装
```bash
# 下载
wget https://www.python.org/ftp/python/3.8.5/Python-3.8.5.tgz
#########演示Python3.8.5安装#########
# 解压并进入源文件
tar -xzvf Python-3.8.5.tgz&&cd Python-3.8.5
# 配置和编译
./configure
# 编译安装(直接安装到 /usr 下面)
make -j16 && sudo make install
# 查看Python3的版本和路径:
python3 -V
which python3
二、自定义路径安装
# 下载
wget https://www.python.org/ftp/python/3.8.5/Python-3.8.5.tgz
#########演示Python3.8.5安装#########
1、解压并进入源文件
tar -xzvf Python-3.8.5.tgz&&cd Python-3.8.5
2、配置和编译
./configure --prefix=/usr/local/python
make -j4
3、安装
sudo make install
4、查看扩展安装包:
/usr/local/python/bin/pip3 list
5、添加Python命令到环境变量:
sudo vim /etc/profile 或者 sudo vim ~/.bashrc
在最后增加一行:
export PATH=$PATH:/usr/local/python/bin
6、使环境变量生效:
source /etc/profile 或者 source ~/.bashrc
7、查看Python3的版本和路径:
python3 -V
which python3
三、更改系统默认python版本
# 更改系统默认python3版本为Python3.8
sudo rm /usr/bin/python3
sudo ln -s /usr/bin/python3.8 /usr/bin/python3
# 更改系统默认python版本为Python3.8
sudo rm /usr/bin/python
sudo ln -s /usr/bin/python3.8 /usr/bin/python
# 查看python当前默认版本
python -V
# 查看 python3 当前默认版本
python3 -V
# 查看所有安装的python路径
whereis python
# 查看所有安装的python3路径
whereis python3
python2.6.9安装
1、下载源文件
wget https://www.python.org/ftp/python/2.6.9/Python-2.6.9.tar.xz
2、解压并进入源文件
tar -xvf Python-2.6.9.tar.xz&&cd Python-2.6.9
3、配置和编译
./configure --prefix=/usr/local/python
make -j16
4、安装
sudo make install
5、更改系统默认python版本为Python2.6
sudo rm /usr/bin/python
sudo ln -s /usr/local/python/bin/python2.6 /usr/bin/python
6、查看python当前默认版本
python -V