linux下python版本默认为python2.7,下面将通过操作在linux系统下修改python的默认版本。
本文linux系统使用的是ubuntu19。
操作:
1.在shell下输入python --version查看当前python版本;
[email protected]:/usr/bin# python --version
Python 2.7.17
[email protected]:/usr/bin#
2.运行命令 ll /usr/bin/pyth* 查看当前linux系统已经安装了哪几个版本的python。
[email protected]:/usr/bin# ll /usr/bin/pyth*
lrwxrwxrwx 1 root root 7 Mar 2 01:52 /usr/bin/python -> python2*
lrwxrwxrwx 1 root root 9 Oct 10 04:32 /usr/bin/python2 -> python2.7*
-rwxr-xr-x 1 root root 3702800 Nov 7 02:07 /usr/bin/python2.7*
lrwxrwxrwx 1 root root 9 Feb 24 03:39 /usr/bin/python3 -> python3.7*
-rwxr-xr-x 1 root root 5082224 Oct 8 09:47 /usr/bin/python3.7*
-rwxr-xr-x 1 root root 5082224 Oct 8 09:47 /usr/bin/python3.7m*
-rwxr-xr-x 1 root root 384 Jan 30 2019 /usr/bin/python3-futurize*
lrwxrwxrwx 1 root root 10 Feb 24 03:39 /usr/bin/python3m -> python3.7m*
-rwxr-xr-x 1 root root 388 Jan 30 2019 /usr/bin/python3-pasteurize*
[email protected]:/usr/bin#
3.如果没有安装python3版本的需要先进行安装;
[email protected]:/usr/bin# apt install python3.7
正在读取软件包列表... 完成
正在分析软件包的依赖关系树
正在读取状态信息... 完成
将会同时安装下列软件:
libpython3.7 libpython3.7-minimal libpython3.7-stdlib python3.7-minimal
建议安装:
python3.7-venv python3.7-doc binfmt-support
下列软件包将被升级:
libpython3.7 libpython3.7-minimal libpython3.7-stdlib python3.7
python3.7-minimal
升级了 5 个软件包,新安装了 0 个软件包,要卸载 0 个软件包,有 103 个软件包未被升级。
4.而在shell中输入命令:python,实际上是读取/usr/bin/python文件,/usr/bin/python文件是一个软链接文件,并指出python2,而python2又是一个指向python2.7的软链接文件。
[email protected]:/usr/bin# ll /usr/bin/python*
lrwxrwxrwx 1 root root 7 Mar 2 01:52 /usr/bin/python -> python2*
lrwxrwxrwx 1 root root 9 Oct 10 04:32 /usr/bin/python2 -> python2.7*
-rwxr-xr-x 1 root root 3702800 Nov 7 02:07 /usr/bin/python2.7*
lrwxrwxrwx 1 root root 9 Feb 24 03:39 /usr/bin/python3 -> python3.7*
-rwxr-xr-x 1 root root 5082224 Oct 8 09:47 /usr/bin/python3.7*
-rwxr-xr-x 1 root root 5082224 Oct 8 09:47 /usr/bin/python3.7m*
-rwxr-xr-x 1 root root 384 Jan 30 2019 /usr/bin/python3-futurize*
lrwxrwxrwx 1 root root 10 Feb 24 03:39 /usr/bin/python3m -> python3.7m*
-rwxr-xr-x 1 root root 388 Jan 30 2019 /usr/bin/python3-pasteurize*
[email protected]:/usr/bin# ll /usr/bin/python
lrwxrwxrwx 1 root root 7 Mar 2 01:52 /usr/bin/python -> python2*
[email protected]:/usr/bin# ll /usr/bin/python2
lrwxrwxrwx 1 root root 9 Oct 10 04:32 /usr/bin/python2 -> python2.7*
[email protected]:/usr/bin#
5.通过更改/usr/bin/python的指向就能实现更改python默认版本,由于软链接文件不支持修改,所以先删除,再重新指向。
删除python软链接文件: rm -rf python
重新创建新指向 python:ln -s /usr/bin/python3 python
[email protected]:/usr/bin#
[email protected]:/usr/bin# rm -rf python
[email protected]:/usr/bin# python --version
bash: /usr/bin/python: 没有那个文件或目录
[email protected]:/usr/bin# ln -s /usr/bin/python3 python
[email protected]:/usr/bin#
6.检查当前python版本;
[email protected]:/usr/bin#
[email protected]:/usr/bin# python
Python 3.7.5rc1 (default, Oct 8 2019, 16:47:45)
[GCC 9.2.1 20191008] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
[email protected]:/usr/bin# python --version
Python 3.7.5rc1
[email protected]:/usr/bin#
7.如果改回python2,同理先删除,再重新创建。
删除python软链接文件: rm -rf python
重新创建新指向 python:ln -s python2 python
[email protected]:/usr/bin#
[email protected]:/usr/bin# rm -rf python
[email protected]:/usr/bin# ln -s python2 python
[email protected]:/usr/bin#
[email protected]:/usr/bin# python --version
Python 2.7.17
[email protected]:/usr/bin#