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
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