How can I use Homebrew to install both Python 2 and 3 on Mac?

后端 未结 9 814
遇见更好的自我
遇见更好的自我 2020-11-29 14:33

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

9条回答
  •  星月不相逢
    2020-11-29 14:55

    I would use pyenv You can install it:

    $ brew install pyenv
    

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

    $ eval "$(pyenv init -)"
    

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

    Usage:

    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
    

    And you can switch between python versions with the command:

    $ pyenv global 3.3.1
    

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

    $ pyenv local 3.5.2
    

    You can check by running python --version:

    $ python --version
    Python 3.5.2
    

    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.

提交回复
热议问题