Ubuntu18.04下python版本完美切换的解决方法

yipeiwu_com6年前Python基础

ubuntu18.04版本,python版本python2.7,python3.5,python3.6

因为安装一些库会安装到python3.6上,而默认使用的是python2.7,使用python3,默认会使用python3.5,无法调用安装包。

解决方法:

一、使用python xx.py运行程序时,加上版本号。比如python3.6 xx.py

二、1.要以root身份操作

yz@yz-pc:~$ sudo su

2.确认本机下的python默认版本。调出终端,输入python即可查看默认的版本:

root@yz-pc:/home/yz# python
Python 3.6.5 (default, Apr 1 2018, 05:46:30) 
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
root@yz-pc:/home/yz# python2.7
Python 2.7.15rc1 (default, Apr 15 2018, 21:51:34) 
[GCC 7.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
root@yz-pc:/home/yz# python3
Python 3.6.5 (default, Apr 1 2018, 05:46:30) 
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
root@yz-pc:/home/yz# python3.5

3.如何切换这两个版本以及切换默认的python版本:

我们可以使用 update-alternatives 来为整个系统更改Python 版本。以 root 身份登录,首先罗列出所有可用的python 替代版本信息:

#update-alternatives --list python
update-alternatives: error: no alternatives for python

如果出现以上所示的错误信息,则表示 Python 的替代版本尚未被update-alternatives 命令识别。想解决这个问题,我们需要更新一下替代列表,将python2.7 和 python3.6放入其中。

​# 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/python python /usr/bin/python3.5 2 
update-alternatives: using /usr/bin/python3.4 to provide /usr/bin/python (python) in auto mode 

--install 选项使用了多个参数用于创建符号链接。最后一个参数指定了此选项的优先级,如果我们没有手动来设置替代选项,那么具有最高优先 级的选项就会被选中。这个例子中,我们为/usr/bin/python3.4 设置的优先级为2,所以update-alternatives 命 令会自动将它设置为默认 Python 版本。

# python --version
Python 3.5.2

接下来,我们再次列出可用的 Python 替代版本。

# update-alternatives --list python
/usr/bin/python2.7
/usr/bin/python3.5

现在开始,我们就可以使用下方的命令随时在列出的 Python 替代版本中任意切换了。

(这一步是最关键的)

# update-alternatives --config python

下面就简单了,会提示你输入序号,你想用哪个版本为默认,就输入序号就可以了!

结束!

参考文章:ubuntu 16.04下python版本切换的方法

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python程序设计入门(3)数组的使用

1、Python的数组可分为三种类型: (1) list 普通的链表,初始化后可以通过特定方法动态增加元素。定义方式:arr = [元素] (2) Tuple 固定的数组,一旦定义后,其...

Python中关于使用模块的基础知识

 一个模块可以在逻辑上组织Python代码。将相关的代码到一个模块中,使代码更容易理解和使用。模块是可以绑定和借鉴任意命名属性的Python对象。 简单地说,一个模块是由Pyt...

Python实现简单字典树的方法

本文实例讲述了Python实现简单字典树的方法。分享给大家供大家参考,具体如下: #coding=utf8 """代码实现了最简单的字典树,只支持由小写字母组成的字符串。 在此代码基...

python3使用matplotlib绘制条形图

python3使用matplotlib绘制条形图

本文实例为大家分享了python3使用matplotlib绘制条形图的具体代码,供大家参考,具体内容如下 绘制竖状条形图 代码 from matplotlib import pyp...

对python当中不在本路径的py文件的引用详解

众所周知,如果py文件不在当前路径,那么就不能import,因此,本文介绍如下两种有效的方法: 方法1: 修改环境变量,在~/.bashrc里面进行修改,然后source ~/.bash...