如何使用Homebrew在Mac上同时安装Python 2和3? [英] How can I use Homebrew to install both Python 2 and 3 on Mac?

查看:170
本文介绍了如何使用Homebrew在Mac上同时安装Python 2和3?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要能够在Python 2和3之间来回切换.我如何使用Homebrew做到这一点,因为我不想弄乱路径并遇到麻烦. 现在我已经通过Homebrew安装了2.7.

I need to be able to switch back and forth between Python 2 and 3. How do I do that using Homebrew as I don't want to mess with path and get into trouble. Right now I have 2.7 installed through Homebrew.

推荐答案

我会使用 pyenv 安装它:

$ brew install pyenv

要在您的Bash shell中启用pyenv,您需要运行:

To enable pyenv in your Bash shell, you need to run:

$ eval "$(pyenv init -)"

要在启动时自动为Bash执行此操作,请将该行添加到您的~/.bash_profile. 1

To do this automatically for Bash upon startup, add that line to your ~/.bash_profile. 1

一旦安装了pyenv并将其激活,就可以安装不同版本的python并选择可以使用的版本.示例:

Once you have installed pyenv and activated it, you can install different versions of python and choose which one you can use. Example:

$ pyenv install 2.7.5

您可以检查已安装的版本:

You can check the versions you have installed with:

$ pyenv versions

您可以使用以下命令在python版本之间进行切换:

And you can switch between python versions with the command:

$ pyenv global 3.3.1

您还可以使用以下命令为当前目录设置python版本:

Also you can set a python version for the current directory with:

$ pyenv local 3.5.2

您可以通过运行python --version进行检查:

You can check by running python --version:

$ python --version
Python 3.5.2


1 Homebrew用来指导您在安装pyenv时执行此操作,但是


1 Homebrew used to instruct you to do this upon installation of pyenv, but the message was removed. For Zsh and other shells, the precise steps may be different.

这篇关于如何使用Homebrew在Mac上同时安装Python 2和3?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆