[Linux] 设置默认python

linux中往往会安装很多个版本的python, 所以会牵扯到默认python的设置问题. 主要是设置系统环境变量的问题.

1. 查看当前默认python版本

直接在terminal中输入”python”

$ python
Python 2.7.14 (default, Sep 23 2017, 22:06:14) 
[GCC 7.2.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 

可以看到当前系统中默认的python版本是 2.7.14

2. 修改默认python版本

下面要把默认的python版本从2.7改为3.6.
进入”/usr/bin”目录下输入”ls -l | grep python”显示所有名字中包含python的文件

$ ls -l | grep python
lrwxrwxrwx 1 root root          26 Mar 18 00:34 dh_pypy -> ../share/dh-python/dh_pypy
-rwxr-xr-x 1 root root        1056 Sep 23  2017 dh_python2
lrwxrwxrwx 1 root root          29 Mar 18 00:34 dh_python3 -> ../share/dh-python/dh_python3
lrwxrwxrwx 1 root root          23 Sep 23  2017 pdb2.7 -> ../lib/python2.7/pdb.py
lrwxrwxrwx 1 root root          23 Mar 18 00:34 pdb3.6 -> ../lib/python3.6/pdb.py
lrwxrwxrwx 1 root root          31 Mar 18 00:34 py3versions -> ../share/python3/py3versions.py
lrwxrwxrwx 1 root root          26 Mar 18 00:34 pybuild -> ../share/dh-python/pybuild
lrwxrwxrwx 1 root root           9 Sep 23  2017 python -> python2.7
lrwxrwxrwx 1 root root           9 Sep 23  2017 python2 -> python2.7
-rwxr-xr-x 1 root root     3617176 Sep 23  2017 python2.7
lrwxrwxrwx 1 root root          33 Sep 23  2017 python2.7-config -> x86_64-linux-gnu-python2.7-config
lrwxrwxrwx 1 root root          16 Sep 23  2017 python2-config -> python2.7-config
lrwxrwxrwx 1 root root           9 Mar 18 00:34 python3 -> python3.6
-rwxr-xr-x 1 root root     4568920 Oct  3 14:45 python3.6
-rwxr-xr-x 1 root root     4568920 Oct  3 14:45 python3.6m
lrwxrwxrwx 1 root root          10 Mar 18 00:34 python3m -> python3.6m
lrwxrwxrwx 1 root root          16 Sep 23  2017 python-config -> python2.7-config
lrwxrwxrwx 1 root root          29 Sep 23  2017 pyversions -> ../share/python/pyversions.py
-rwxr-xr-x 1 root root        2971 Sep 23  2017 x86_64-linux-gnu-python2.7-config
lrwxrwxrwx 1 root root          33 Sep 23  2017 x86_64-linux-gnu-python-config -> x86_64-linux-gnu-python2.7-config

注意其中有个link文件(链接文件, 所谓的快捷方式)python指向的是python2.7

lrwxrwxrwx 1 root root           9 Sep 23  2017 python -> python2.7

只要把它的指向改为python3即可

$ sudo rm -rf python
$ sudo ln -s /usr/bin/python3  /usr/bin/python

最后再在terminal中输入”python”

$ python
Python 3.6.3 (default, Oct  3 2017, 21:45:48) 
[GCC 7.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 

可以看到默认python版本已经变为3.6.3了.

猜你喜欢

转载自blog.csdn.net/pangtouyu_qy/article/details/79765691
今日推荐