24

I'm running MacOS Sierra 10.12.4 and I've realized that homebrew python was upgraded to version 2.7.13. How can I switch back to 2.7.10?

13
  • 4
    Never touch the system python. Install your own independent Python version. Commented May 27, 2017 at 13:37
  • And why do you think you need to downgrade to 2.7.10? What problems does 2.7.13 give you, can those be addressed in a different way? Commented May 27, 2017 at 13:37
  • 1
    Last but not least, what has this got to do with homebrew? Commented May 27, 2017 at 13:38
  • Acrually I'm not sure version 2.7.13 is the problem, so I wanted to make sure trying to use version 2.7.10. So if I install my independent python version, how can I then set it as system default?
    – leota
    Commented May 27, 2017 at 13:42
  • 1
    Sounds like you are asking the wrong question perhaps. Commented May 27, 2017 at 13:51

6 Answers 6

29

You can switch versions with brew switch. For instance I just downgraded Python 3.7.0 to 3.6.5 like this:

brew switch python 3.6.5

Unfortunately, the brew versions command has been deprecated, and it's currently pretty complicated to locate the available versions. I'd love to hear a simple solution to this. Meanwhile, if you know the version you want to switch to, try the above command.

I agree with the answers here that virtualenvs are a good idea, but having the version of Python you need in homebrew is also a good idea. The way my virtualenvs were created, bin/python was a symlink to /usr/local/bin/python, so things broke when Python was updated via homebrew.

3
  • 5
    Error: python does not have a version "3.6.5" in the Cellar.
    – Jeereddy
    Commented Oct 24, 2018 at 23:31
  • The line below the line you mentioned should tell you what version of python are installed in the Cellar. $ brew switch python 3.6.5 Error: python does not have a version "3.6.5" in the Cellar. python installed versions: 3.6.5_1, 3.7.0
    – tsmets
    Commented Oct 25, 2018 at 23:23
  • 7
    Apparently, the switch option has been removed from Homebrew. I solved it by using brew link [email protected].
    – fedorqui
    Commented Jul 27, 2021 at 14:53
14

First, it's generally considered bad practice to rely on system python for user land code if you can avoid it. You need to assume that system utilities require a specific version of system python, and your user land code may then be locked to that python version forever, which is not wise (unless you're writing system utilities, in which case just use /bin/python, but then you wouldn't be asking this question...).

Secondly, I am unclear why you need 2.7.10 instead of 2.7.13. All pythons with the same minor revision number (2.7) should always be compatible. If you needed 2.6, that would be a different story since that's a change in minor version. Code written for 2.7.x should all be compatible.

However, assuming your use case really does require using a specific Python version - getting to an actual solution now - be sure sure you really upgraded system python to begin with. If you enter the command: which python, do you get /usr/bin/python (system) or /usr/local/bin/python (brew installed user-land python). For example, /usr/bin/python -V gives me 2.7.10 even though python -V gives me 2.7.13 (via brew).

It's possible that you installed the latest python 2.7.x via brew which puts /usr/local/bin/python as a symlink in your $PATH, or you perhaps have a python alias pointing somewhere you don't want. Verify your $PATH order.

You can reset your homebrew python by removing it (brew uninstall python), or by changing the symlink (ln -s -f /usr/bin/python /usr/local/bin/python). However, using virtualenv removes the need for much of these sorts of gymnastics.

If you want to monkey with prior versions of Python installed via homebrew, this answer should help: How to install older formula using Brew?

One final option: if you absolutely must have a specific python version, pyenv can help.

brew install pyenv
pyenv install 2.7.10
pyenv global 2.7.10
6
  • 1
    homebrew/versions was deprecated :(
    – leota
    Commented May 27, 2017 at 14:33
  • 3
    after "pyenv global 2.7.10" if I do "python -V" still get 2.7.13
    – leota
    Commented May 27, 2017 at 14:50
  • 2
    brew switch says: python does not have a version "2.7.10" in the Cellar.
    – leota
    Commented May 27, 2017 at 14:50
  • 4
    pyenv works by modifying your PATH. You need to make sure that export PATH=$(pyenv root)/shims:$PATH made it into your .bash_profile per the instructions github.com/pyenv/pyenv
    – mattmc3
    Commented May 27, 2017 at 14:52
  • 1
    Fantastic! it works now. So that was the problem, I needed python 2.7.10. thanks a looooot
    – leota
    Commented May 27, 2017 at 14:58
2

Download python 3.6.0 from https://www.python.org/downloads/release/python-360/

Install it as a normal package.

Run cd /Library/Frameworks/Python.framework/Version

Run ls and all installed Python versions will be visible here.

Run sudo rm -rf 3.7

Check the version now by python3 -V and it will be 3.6 now.

0

There is no need to downgrade python as you can use both on your system.

Places where you want your file to compile with python 2-x.

python2 or python2-x filename.py

and where you need python 3

python3 or python3-x filename.py

The default usage of python will lead to use the latest version, and downgrading to a particular version is a lot of headache as it is not direct as python is not made backward compatible from 3-x to 2-x.

0

If brew link python@<VERSION> doesn't work for you.

You can always remove the problematic version with:

brew uninstall python@<VERSION>
-1

This is not a direct answer to the question, rather it explains a solution to avoid touching the system python.

The general idea is that you should always install the independent python for your projects. Each project needs its own python version (for compatibility reasons with libraries), and it's not practical to keep one python version and trying to make it work with multiple projects.

I assume this issue in your system happened because another project required a higher python version and now for your other project you need a lower version python.

The best way to handle python versions is to use virtualenv.

Each project will have it's own python, so you can have projects that work with python 2.7 and python 3 and they never touch each other's dependency.

Install different python versions with homebrew and then for each project when you create virtualenv you decide which python to pick. Everytime you work with that project, the python version would be the one you picked yourself when created the virtualenv.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.