153

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.

Raj
  • 22,346
  • 14
  • 99
  • 142
MostafaMV
  • 2,181
  • 3
  • 16
  • 22
  • You might want to change the question to remove homebrew reference since it is not necessarily a requirement to accomplish your goal (see my answer below for example https://stackoverflow.com/a/49091638/2923369) – Matt Schlobohm Mar 04 '18 at 03:28

9 Answers9

299

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.

Flimm
  • 136,138
  • 45
  • 251
  • 267
moliware
  • 10,160
  • 3
  • 37
  • 47
  • This is the simplest solution. I currently use this for switching between Python 3 and 2.7.x – Naz Mir Sep 07 '13 at 08:22
  • Thanks. so how do I switch back and forth? should I pyenv the version I want to use each time I wanna switch? – MostafaMV Sep 07 '13 at 08:25
  • 3
    If I have already installed python 2.7 through *homebrew*, is there anything I should take care of using *pyenv*? Can I install another python 2.7 through *pyenv*? – Drake Guan Oct 29 '13 at 11:04
  • 3
    Yep, you can install another python 2.7 through pyenv. It will install that python in $PYENV_ROOT/versions. The other python 2.7 installed through homebrew may appear as "system" when you execute "pyenv versions". – moliware Oct 29 '13 at 11:18
  • I have installed `python2` and `python3` with homebrew. After installing `pyenv` and setting a `pyenv global 3.4.2` python3 is still linked to the previous python3 and I only see one version for system – ilciavo Jan 06 '15 at 18:03
  • 7
    I had to add `eval "$(pyenv init -)"` to my .profile/.zshrc to get pyenv working. – tim-phillips Dec 27 '15 at 18:16
  • 5
    @ilciavo, you may have to add `eval "$(pyenv init -)"` to your ~/.bash_profile – Jojanzing Apr 03 '16 at 14:35
  • `brew install pyenv` recommends something that works like that, @Rohmer @Jojanzing – Greg Dubicki Apr 17 '16 at 11:20
  • once use pyenv to install a version of python, is there way to have the system default to using that version? – dtburgess Jun 14 '16 at 12:57
  • If you use zsh, you should follow the [README on pyenv](https://github.com/pyenv/pyenv#basic-github-checkout). – John Huang Nov 06 '17 at 05:37
  • Does this still work with a Homebrew from after 1 March 2018? – acidjunk Mar 27 '18 at 10:43
  • Note that you may have to uninstall `binutils` from Brew on an M1 Macbook in order for pyenv to install 2.7 successfully. It was failing for me with `ld: symbol(s) not found for architecture arm64` – Matthew Read Apr 11 '22 at 15:51
77

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 python@2 formula will be added for Python 2.7, specifically.

See changes announcement here or the final doc about using Homebrew for Python here.

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 python3 executable.

p-a-o-l-o
  • 9,807
  • 2
  • 22
  • 35
Raj
  • 22,346
  • 14
  • 99
  • 142
  • the question is about using both – meduz May 05 '15 at 08:07
  • thanks for the update! I followed a similar route but had problems with having to set ``PYTHONPATH``at each change... any idea? besides that, ``pyenv`` is great. – meduz May 06 '15 at 20:51
  • 9
    Also, note that having python2 and python3 side by side, that python points to python2 and python3 points to python3 as expected. However, a big gotcha is that pip does not point to pip2 as you might expect. It actually points to pip3 (as does pip3 obviously). To use pip2, explicitly use pip2. A bit inconsistent having the non number suffix for python point to 2 but the non number suffix for pip points to 3. – J.D. Feb 01 '16 at 02:35
  • 5
    This messes up common executables like pip. python3 will overwrite python2 pip in this case and that will be the default "pip" command – kissgyorgy Mar 15 '16 at 12:13
  • My copy of OS X came with 2.7.10, but Homebrew installed 2.7.11. However, my machine still runs 2.7.10 by default. Is there a way to change the default to the copy Homebrew installed without pyenv or changing my path? – nipponese Mar 26 '16 at 01:12
  • @nipponese - You have to change your PATH, that's how the shell works. So make sure your `PATH` environment variable includes the Brew bin folder `/usr/local/bin` at the *front* of the path. Put this in `~/.bash_profile` or similar then `source ~/.bashrc`. Then type `hash -r` to re-hash your bash shell (due to path changing). Now `type python` should show `/usr/local/bin/path` and `python --version` should show the brew-installed version. See [this question](http://stackoverflow.com/a/7375583/992887) for more. – RichVel Aug 28 '16 at 05:48
  • 1
    In response to @kissgyorgy's comment: I had that issue, the last comment on this issue resolved it for me https://github.com/Homebrew/legacy-homebrew/issues/50607, running `python3 -m pip install -U --force-reinstall pip` then `python -m pip install -U --force-reinstall pip` put things back to having `pip` refer to Python 2's `pip`. – Harry Feb 28 '17 at 12:38
  • 2018 update: https://brew.sh/2018/01/19/homebrew-1.5.0/ Now `python` will default to Python 3, and to have both versions, Python 2.x is available with `python@2` – wont_compile Mar 07 '18 at 14:05
  • python@2 was deleted from homebrew as of January 1, 2020. – cdabel Mar 05 '20 at 21:10
12

Currently Homebrew provides two different formulas for Python 2 and 3. brew install python installs python3, and brew install python@2 installs python2. More details in Homebrew docs:

https://docs.brew.sh/Homebrew-and-Python

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:

python --version

and

python3 --version

To see what versions of Python 2.x and 3.x installed.

jpayne
  • 102
  • 2
  • 9
Guilherme Garnier
  • 2,208
  • 23
  • 22
6

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.

Fred Mitchell
  • 2,145
  • 2
  • 21
  • 29
6

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
ishandutta2007
  • 16,676
  • 16
  • 93
  • 129
2

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

https://github.com/asdf-vm/asdf
https://github.com/tuvistavie/asdf-python
https://github.com/asdf-vm/asdf-plugins

Community
  • 1
  • 1
Matt Schlobohm
  • 1,312
  • 12
  • 15
  • Seems to me that these are not competing tools but the same tool, implemented once (and sometimes more than once) per language. Kind of like why we need something like the LSP as well, IMHO. https://microsoft.github.io/language-server-protocol/overview – Matt Schlobohm Jun 21 '18 at 04:26
1

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

git clone https://git.<theThingYouWantToInstall>

Depending on the repo, check if there is MAKE file already setup for this option.

rustyMagnet
  • 3,479
  • 1
  • 31
  • 41
1

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.

Christian Vielma
  • 15,263
  • 12
  • 53
  • 60
-3

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.

Vladimir Stazhilov
  • 1,956
  • 4
  • 31
  • 63