python --verstion,或python3 -V
ls -l /usr/bin | grep python
此命令信息较详细,也可使用简洁命令:ls /usr/bin/python*
从版本2升级到3,可使用命令:apt-get install python3.7。若报错,只能从源码安装。
若报此错E: Couldn't find any package by regex 'python3.7-dev'则sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt-get update
sudo apt-get install python3.7
或
sudo add-apt-repository ppa:jonathonf/python-3.7
sudo apt-get update
sudo apt-get install python3.7
若上述运行sudo add-apt-repository ppa:deadsnakes/ppa 出现 “bash: add-apt-repository: command not found” 则先运行:
sudo apt-get install -y software-properties-common
update-alternatives --list python#查看可替代python版本,如果报错没有可替代版本,则需要更新替代列表(操作如下)
update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1
update-alternatives --install /usr/bin/python python /usr/bin/python3.5 2
若报错:update-alternatives: error: no alternatives for python,说明python 的替代版本尚未被 update-alternatives 命令识别,要更新一下替代列表,将 python2.7 和 python3.*放入其中。
# update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1
update-alternatives: using /usr/bin/python2.7 to provide /usr/bin/python (python) in auto mode
# update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.4 2
update-alternatives: using /usr/bin/python3.4 to provide /usr/bin/python (python) in auto mode
加入新版python3.12的地址是,要使用真实地址,查找方法:which python3.12,即:/usr/local/bin/python3.12
也可移除代替版本:
# update-alternatives --remove python /usr/bin/python2.7
update-alternatives: removing manually selected alternative - switching python to auto mode
update-alternatives: using /usr/bin/python3.4 to provide /usr/bin/python (python) in auto mode
update-alternatives --config python#选择标号
直接输入python3会链接指向默认python文件夹,可修改的。若之前python3指向python3.6.9,现修改成python3.7.1。
ls -l /usr/bin | grep python3
rm /usr/bin/python3
不一定全在/usr/bin下,which python3
ln -s /usr/bin/python3.7.1 /usr/bin/python3
python3
sudo apt-get update
sudo apt-get install zlib1g-dev libbz2-dev libssl-dev libncurses5-dev libsqlite3-dev libreadline-dev tk-dev libgdbm-dev libdb-dev libpcap-dev xz-utils libexpat1-dev liblzma-dev libffi-dev libc6-dev
wget https://www.python.org/ftp/python/3.12.0/Python-3.12.0.tgz
解压tar -xzf Python-3.12.0.tgz,进入cd Python-3.12.0
sudo ./configure --prefix=/usr/local/python3.12
若出现:If you want a release build with all stable optimizations active (PGO, etc), please run ./configure --enable-optimizations
则执行sudo ./configure --enable-optimizations
sudo make
sudo make install
若最后出现:Successfully installed pip-23.2.1,即安装成功,此时可以输入python3 -V验证,一般不需要软连接,也就是说会自动更新pip3。
为了减少安装量,最开始,也可以不执行步骤1,若在步骤6中报错ModuleNotFoundError: No module named _ctypes,执行如下:
sudo apt-get update
sudo apt-get upgrade
sudo apt-get dist-upgrade
sudo apt-get install build-essential python-dev python-setuptools python-pip python-smbus build-essential libncursesw5-dev libgdbm-dev libc6-dev zlib1g-dev libsqlite3-dev tk-dev libssl-dev openssl libffi-dev
再执行步骤6:sudo make install