主页 M

Ubuntu中python在线升级,源码安装最新版及多版本切换,换链接指向

2019-12-17 网页编程网 网页编程网

1.查看python版本

python --verstion,或python3 -V

2.查看python版本指向

ls -l /usr/bin | grep python

此命令信息较详细,也可使用简洁命令:ls /usr/bin/python*

3.直接升级

从版本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

4.切换python版本

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

5.进行切换

update-alternatives --config python#选择标号

6.换链接指向

直接输入python3会链接指向默认python文件夹,可修改的。若之前python3指向python3.6.9,现修改成python3.7.1。

6.1查看python命令指向

ls -l /usr/bin | grep python3

6.2删除原有链接

rm /usr/bin/python3

6.3查找python3.7.1文件夹路径

不一定全在/usr/bin下,which python3

6.4建立新链接

ln -s /usr/bin/python3.7.1 /usr/bin/python3

6.5验证

python3

7.源码安装python3.12.0

1.更新源及依赖

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

2.下载Python源代码

wget https://www.python.org/ftp/python/3.12.0/Python-3.12.0.tgz

3.解压并进入目录

解压tar -xzf Python-3.12.0.tgz,进入cd Python-3.12.0

4.配置安装位置

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

5.编译

sudo make

6.安装

sudo make install 

7.验证

若最后出现:Successfully installed pip-23.2.1,即安装成功,此时可以输入python3 -V验证,一般不需要软连接,也就是说会自动更新pip3。

8.总结

为了减少安装量,最开始,也可以不执行步骤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

阅读原文
阅读 3911
123 显示电脑版