$ brew install pyenv
pyenv安装完以后,就可以选择性的进行Python环境安装了。下面已安装Python2.7为例:
$ pyenv install 2.7.5
此外,你还可以通过pyenv查看目前系统中已经安装过的Python版本
$ pyenv versions
如果需要在不同版本的Python间进行切换的话,使用以下命令:
$ pyenv global 3.3.1
当然,你也可以让版本切换只对当前目录生效
$ pyenv local 2.7.5
pyenv install出错
错误信息:zipimport.ZipImportError: can't decompress datazlib not available解决方法:xcode-select --install,然后安装命令行工具(即使你安装了xcode)
Python2.7是Python2版本,Python3.x才是Python3版本。
Python2与Python3的主要区别:
区别一:print语法使用
Python2.7 print语法使用 >>>print "Hello Python"
Python3.7 print语法使用 >>>print("Hello Python")
区别二: raw_input()和input()
Python 2.7 raw_input() input() 都存在 可使用 raw_input()接收字符串string input()接收数字int /flot.
Python 3.7 raw_input()不存在 仅存在input() 两者合并 接收任意格式 返回string
区别三: 函数cmp()
python 2.7 cmp(x,y)函数用于比较2个对象,如果 x <y 返回 -1, 如果 x == y 返回 0, 如果 x >y 返回 1
python3.7 cmp()已经不存在了,如果你需要实现比较功能,需要引入 operator 模块,适合任何对象
update-alternatives --install /usr/bin/python python /usr/bin/python2 1 #将默认版本优先级设为1update-alternatives --install /usr/bin/python python /usr/bin/python3 2 #将python3优先级设为2
python -V #可以看到python默认版本已经替换
本文链接自 http://www.myhack58.com/Article/48/66/2016/71806.htm
当你安装 Debian Linux 时,安装过程有可能同时为你提供多个可用的 Python 版本,因此系统中会存在多个 Python 的可执行二进制文件。你可以按照以下方法使用 ls 命令来查看你的系统中都有那些 Python 的二进制文件可供使用。
会看到类似下列信息:
/usr/bin/python /usr/bin/python2 /usr/bin/python2.7 /usr/bin/python3 /usr/bin/python3.4 /usr/bin/python3.4m /usr/bin/python3m
$ python --version
Python 2.7.8
想要为某个特定用户修改 Python 版本,只需要在其 home 目录下创建一个 alias(别名) 即可。打开该用户的 ~/.bashrc 文件,添加新的别名信息来修改默认使用的 Python 版本。
我们可以使用 update-alternatives 来为整个系统更改 Python 版本。以 root 身份登录,首先罗列出所有可用的 python 替代版本信息:
update-alternatives: error: no alternatives for python
如果出现以上所示的错误信息,则表示 Python 的替代版本尚未被 update-alternatives 命令识别。想解决这个问题,我们需要更新一下替代列表,将 python2.7 和 python3.4 放入其中。
1、# 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
2、# update-alternatives --install /usr/bin/python python /usr/bin/python3.4 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 版本。
1、 # python --version
接下来,我们再次列出可用的 Python 替代版本。
1、# update-alternatives --list python
2、 /usr/bin/python2.7
3、 /usr/bin/python3.4
现在开始,我们就可以使用下方的命令随时在列出的 Python 替代版本中任意切换了。
1、 # update-alternatives --config python
$ python --version
Python 2.7.8
一旦我们的系统中不再存在某个 Python 的替代版本时,我们可以将其从 update-alternatives 列表中删除掉。例如,我们可以将列表中的 python2.7 版本移除掉。
1、 # update-alternatives --remove python /usr/bin/python2.7
2、 update-alternatives: removing manually selected alternative - switching python to auto mode
3、 update-alternatives: using /usr/bin/python3.4 to provide /usr/bin/python (python) in auto mode