ubuntu16.04默认的软件repository中并不包含python3.6(最高到python3.5),所以无法通过下面命令直接安装python3.6
apt-get install python3.6
当你强制执行上述命令是会出现如下错误:
E: Unable to locate package python3.6-dev
E: Couldn't find any package by glob 'python3.6-dev'
E: Couldn't find any package by regex 'python3.6-dev'
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt-get update
sudo apt-get install python3.6
or
sudo add-apt-repository ppa:jonathonf/python-3.6
sudo apt-get update
sudo apt-get install python3.6
若上述运行sudo add-apt-repository ppa:deadsnakes/ppa 出现 “bash: add-apt-repository: command not found” 则先运行:
sudo apt-get install -y software-properties-common
ubuntu默认内置了python2.7版本,当你在终端输入python时,默认是2.7的版本,那如何将python3.6版本设置为默认版本呢(对于其他版本同样使用)?请往下看:
查看python版本的优先级
sudo update-alternatives --config python
若没有设置优先级,则会显示如下error:
update-alternatives: error: no alternatives for python3
此时你可以通过update-alternatives来设置默认python版本, 最后的参数1,2是优先级,数字越大优先级越高,比如经过如下设置后,在终端输入python,显示的就是3.6的版本了。
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.5 1
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.6 2
你也可以另外一种方式来设置python的默认版本:
sudo update-alternatives --set python /usr/bin/python3.6
上述方式在终端输入python,会默认使用你设置的版本,但是如果你想在终端输入python时显示2.7的版本,输入python3时才是你想要的版本时,你就需要关联一下版本。
也许你会碰到明明通过apt-get install python3.6 安装了python3版本,但是在终端输入python3时却提示:
bash: /usr/bin/python3: No such file or directory
此时就需要做关联了,做好关联后也相当于设置了默认参数。
1. 打开.bashrc
vim ~/.bashrc
2. 在.bashrc中添加
alias python3=python3.6
or
alias python3='/usr/bin/python3.6'
3. 保存并退出文件编辑,使配置生效
source ~/.bashrc
再在终端输入python3时,就会显示3.6的版本(因为上面配的就是3.6的版本)
为python3.6安装对应的pip
curl https://bootstrap.pypa.io/ez_setup.py -o - | python3.6 && python3.6 -m easy_install pip
or
curl "https://bootstrap.pypa.io/get-pip.py" -o "get-pip.py" python3 get-pip.py --user
sudo apt autoremove python3.6