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

后端 未结 9 816
遇见更好的自我
遇见更好的自我 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 15:05

    There are ways to use both , but the simplest solution today is to use pyenv. pyenv allows easy switching between versions. Here is what I did to set up:

    STEP1:

    Remove all pythons from your mac

     brew uninstall --ignore-dependencies --force python
     sudo rm -rf ~/miniconda3/
     sudo rm -rf ~/.conda/
    

    Remove the following from ~/.bash_profile

    export PATH="/Users/ishandutta2007/miniconda3/bin:$PATH"

    and also the following from ~/.bashrc

    export PYTHONPATH=/usr/local/lib/python2.7/site-packages:$PYTHONPATH
    export PYTHONPATH=/usr/local/lib/python2.7/site-packages/google:$PYTHONPATH
    alias python="/usr/bin/python"
    

    STEP2:

    Install pyenv and the python versions you need

    brew update
    brew install pyenv
    pyenv install 2.7
    pyenv install 3.7.0
    

    STEP3:

    add pyenv init to bash_profile or bashrc

    echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n  eval "$(pyenv init -)"\nfi' >> ~/.bash_profile
    

    STEP4:

    Check what got installed

    pyenv versions
    
    • system (set by /Users/ishandutta2007/.pyenv/version)

      2.7

      3.7.0

    STEP5:

    Choose a default

    pyenv global 3.7.0
    

    When a project needs older version, just go its root folder and run

    pyenv local 2.7
    

提交回复
热议问题