21

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?

leota
  • 1,636
  • 2
  • 13
  • 33
  • 4
    **Never** touch the system python. Install your own independent Python version. – Martijn Pieters May 27 '17 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? – Martijn Pieters May 27 '17 at 13:37
  • 1
    Last but not least, what has this got to do with homebrew? – Martijn Pieters May 27 '17 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 May 27 '17 at 13:42
  • You *don't* set it as the system default. Various OS X programs use the system default and rely on a specific version. Have your 'problem' code run with the separate Python version instead. – Martijn Pieters May 27 '17 at 13:43
  • I'm trying to import a module, which normally is used by a 3D package which rely on python 2.7.10. The error I'm getting is "Expected in: flat namespace", which I know it has something to do with c++ libs, but it used to work before – leota May 27 '17 at 13:48
  • Please explain how you "realized" it upgraded – OneCricketeer May 27 '17 at 13:51
  • 1
    Sounds like you are asking the wrong question perhaps. – Martijn Pieters May 27 '17 at 13:51
  • doing a python -V – leota May 27 '17 at 13:54
  • And how do you know that is the system Python, not homebrew? (assuming you are using it, since it is tagged) – OneCricketeer May 27 '17 at 14:08
  • system python and the one installed via homebrew are not the same? – leota May 27 '17 at 14:15
  • No, system python is the one that comes built into OS X. – pvg May 27 '17 at 14:47
  • homebrew installs to /usr/local by default. Run `which python` to find out the location which will be called by default. If `/usr/local/bin/python` you are probably getting the homebrew version. If `/usr/bin/python` you are getting the system version. – Joseph Sheedy Dec 05 '18 at 19:20

5 Answers5

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.

Joseph Sheedy
  • 6,296
  • 4
  • 30
  • 31
  • 5
    `Error: python does not have a version "3.6.5" in the Cellar.` – Jeereddy Oct 24 '18 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 Oct 25 '18 at 23:23
  • 4
    Apparently, the `switch` option has been removed from Homebrew. I solved it by using `brew link python@3.8`. – fedorqui Jul 27 '21 at 14:53
11

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
mattmc3
  • 17,595
  • 7
  • 83
  • 103
  • I'm wrong, I was thinking that system python and the one installed by brew were the same. I'll edit my question. So in this case I mean the homebrew one. How can I downgrade that? – leota May 27 '17 at 14:22
  • 1
    homebrew/versions was deprecated :( – leota May 27 '17 at 14:33
  • 3
    after "pyenv global 2.7.10" if I do "python -V" still get 2.7.13 – leota May 27 '17 at 14:50
  • 2
    brew switch says: python does not have a version "2.7.10" in the Cellar. – leota May 27 '17 at 14:50
  • 2
    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 https://github.com/pyenv/pyenv – mattmc3 May 27 '17 at 14:52
  • 1
    Fantastic! it works now. So that was the problem, I needed python 2.7.10. thanks a looooot – leota May 27 '17 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.

Sidharth Taneja
  • 548
  • 6
  • 7
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.

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

apadana
  • 13,456
  • 15
  • 82
  • 98