![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiInBnauEXenlmMklWa2RWeplWe69WNzkDMwIzLcJDMvwlMwYTMwIzLc1WdixWYvwFduVWboNWY0RXYvwVY0FGZvwVZt5CevJWcu42Y4VnbpxWLuR2Lc9CX6MHc0RHaiojIsJye.jpg)
当你安装 debian linux 时,安装过程有可能同时为你提供多个可用的 python 版本,因此系统中会存在多个 python 的可执行二进制文件。你可以按照以下方法使用 <code>ls</code> 命令来查看你的系统中都有那些 python 的二进制文件可供使用。
<code>$ ls /usr/bin/python*</code>
<code>/usr/bin/python /usr/bin/python2 /usr/bin/python2.7 /usr/bin/python3 /usr/bin/python3.4 /usr/bin/python3.4m /usr/bin/python3m</code>
执行如下命令查看默认的 python 版本信息:
<code>$ python --version</code>
<code>python 2.7.8</code>
<a></a>
想要为某个特定用户修改 python 版本,只需要在其 home 目录下创建一个 <code>alias</code>(别名) 即可。打开该用户的<code>~/.bashrc</code> 文件,添加新的别名信息来修改默认使用的 python 版本。
<code>alias python='/usr/bin/python3.4'</code>
一旦完成以上操作,重新登录或者重新加载 <code>.bashrc</code> 文件,使操作生效。
<code>$ . ~/.bashrc</code>
检查当前的 python 版本。
<code>python 3.4.2</code>
我们可以使用 <code>update-alternatives</code> 来为整个系统更改 python 版本。以 root 身份登录,首先罗列出所有可用的 python 替代版本信息:
<code># update-alternatives --list python</code>
<code>update-alternatives: error: no alternatives for python</code>
如果出现以上所示的错误信息,则表示 python 的替代版本尚未被 <code>update-alternatives</code> 命令识别。想解决这个问题,我们需要更新一下替代列表,将 <code>python2.7</code> 和 <code>python3.4</code> 放入其中。
<code># update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1</code>
<code>update-alternatives: using /usr/bin/python2.7 to provide /usr/bin/python (python) in auto mode</code>
<code># update-alternatives --install /usr/bin/python python /usr/bin/python3.4 2</code>
<code>update-alternatives: using /usr/bin/python3.4 to provide /usr/bin/python (python) in auto mode</code>
<code>--install</code> 选项使用了多个参数用于创建符号链接。最后一个参数指定了此选项的优先级,如果我们没有手动来设置替代选项,那么具有最高优先级的选项就会被选中。这个例子中,我们为 <code>/usr/bin/python3.4</code> 设置的优先级为2,所以 <code>update-alternatives</code> 命令会自动将它设置为默认 python 版本。
<code># python --version</code>
接下来,我们再次列出可用的 python 替代版本。
<code>/usr/bin/python2.7</code>
<code>/usr/bin/python3.4</code>
现在开始,我们就可以使用下方的命令随时在列出的 python 替代版本中任意切换了。
<code># update-alternatives --config python</code>
一旦我们的系统中不再存在某个 python 的替代版本时,我们可以将其从 <code>update-alternatives</code> 列表中删除掉。例如,我们可以将列表中的 python2.7 版本移除掉。
<code># update-alternatives --remove python /usr/bin/python2.7</code>
<code>update-alternatives: removing manually selected alternative - switching python to auto mode</code>
本文来自云栖社区合作伙伴“linux中国”
原文发布时间为:2013-04-02.