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.
Here is Solutions:
We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.
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
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 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.
You can have both versions installed at the same time.
For Homebrew >=1.5.0:
Since 1st March 2018 the
python formula will be upgraded to Python 3.x, while a new
[email protected] formula will be added for Python 2.7, specifically.
For older Homebrew:
For Python 2.x:
brew install python
For Python 3.x:
brew install python3
Now, you will have both the versions installed in your machine. When you want to use version 2, use the
python executable. When you want to use version 3, use the
Currently Homebrew provides two different formulas for Python 2 and 3.
brew install python installs python3, and
brew install [email protected] installs python2. More details in Homebrew docs:
If you currently have 2.x installed via Homebrew, Homebrew will give you a message such as:
Error: python 2.7.14 is already installed To upgrade to 3.6.5, run `brew upgrade python`
If you run:
brew upgrade python
you should be able to do:
To see what versions of Python 2.x and 3.x installed.
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:
Remove all pythons from your mac
brew uninstall --ignore-dependencies --force python sudo rm -rf ~/miniconda3/ sudo rm -rf ~/.conda/
Remove the following from
and also the following from
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"
Install pyenv and the python versions you need
brew update brew install pyenv pyenv install 2.7 pyenv install 3.7.0
pyenv init to
echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n eval "$(pyenv init -)"\nfi' >> ~/.bash_profile
Check what got installed
system (set by /Users/ishandutta2007/.pyenv/version)
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
Alternatively, you probably can just enter “python3” to run your most current version of python3.x and “python” or “python2” to run the latest installed 2.x version.
Use asdf !
Ballad of asdf
Once upon a time there was a programming language
There were many versions of it
So people wrote a version manager for it
To switch between versions for projects
Different, old, new.
Then there came more programming languages
So there came more version managers
And many commands for them
I installed a lot of them
I learnt a lot of commands
Then I said, just one more version manager
Which I will write instead
So, there came another version manager
asdf version manager – https://github.com/asdf-vm/asdf
A version manager so extendable
for which anyone can create a plugin
To support their favourite language
No more installing more version managers
Or learning more commands
I thought I had the same requirement – to move between Python versions – but I achieved all I needed with only Python3.6 by building from source instead of using
git clone https://git.<theThingYouWantToInstall>
Depending on the repo, check if there is MAKE file already setup for this option.
I was able to just go to https://www.python.org/downloads/mac-osx/ and download the latest python. It installed along side current python in my system.
Okay, I was struggling with my brew installation of Python3, because I didn’t have pip3
sudo pip3 command not found
and so I did
brew uninstall --force --ignore-dependencies python3
and installed the regular Python 3.6.2 from official distribution and then I had pip3 and all components were ok.
Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂