Skip to content

Instantly share code, notes, and snippets.

@Bouke
Last active January 7, 2026 00:43
Show Gist options
  • Select an option

  • Save Bouke/11261620 to your computer and use it in GitHub Desktop.

Select an option

Save Bouke/11261620 to your computer and use it in GitHub Desktop.
Multiple Python installations on OS X

Previous versions used homebrew to install the various versions. As suggested in the comments, it's better to use pyenv instead. If you are looking for the previous version of this document, see the revision history.

$ brew update
$ brew install pyenv
$ pyenv install 3.5.0
$ pyenv install 3.4.3
$ pyenv install 3.3.6
$ pyenv install 3.2.6
$ pyenv install 2.7.10
$ pyenv install 2.6.9
$ pyenv versions
* system (set by /Users/bouke/.pyenv/version)
  2.6.9
  2.7.10
  3.2.6
  3.3.6
  3.4.3
  3.5.0

Then, when you need a certain version:

pyenv local 3.5.0

This will create a file .python-version, so pyenv will remember the Python version for this directory. This works also nice with tox, as you can activate many Python version at once. See the pyenv documentation for more information.

See also Using tox and pyenv for testing and development for a more detailed how-to.

@tmehlinger

Copy link
Copy Markdown

The Python 3.2 install will fail on newer versions of Homebrew because the formula is incompatible. I had to comment out lines 133-141 of Library/Formula/python3.rb and install Setuptools and Pip manually after the Python install finished.

Other than that, worked perfectly, thanks for taking the time to document this. :)

@IvanExpert

Copy link
Copy Markdown

Thanks for this writeup -- very helpful. Also thanks tmhlinger for your tip.

@Bouke

Bouke commented Apr 7, 2015

Copy link
Copy Markdown
Author

The brew versions command has been removed. The instructions are no longer valid.

@zoidyzoidzoid

Copy link
Copy Markdown

This worked perfectly for me, I just skipped the brew versions line.

@tdsmith

tdsmith commented Sep 14, 2015

Copy link
Copy Markdown

I'm a Homebrew maintainer; I strongly suggest you use pyenv instead if you ever need anything but the most current Python versions. It's a much nicer solution in part because it's designed to handle switching scripts back and forth, which is very difficult for Homebrew to do.

@Bouke

Bouke commented Sep 18, 2015

Copy link
Copy Markdown
Author

Thanks, I didn't know about pyenv, but found very useful already. I've updated the document as this looks like a better way to do this.

@den-run-ai

Copy link
Copy Markdown

The link is broken:

Using tox and pyenv for testing and development
http://tbb.co/using-tox-and-pyenv-for-testing-and-development/

@den-run-ai

Copy link
Copy Markdown

I believe the new link is now here:

http://www.holger-peters.de/using-pyenv-and-tox.html

@Bouke

Bouke commented Sep 28, 2016

Copy link
Copy Markdown
Author

Thanks @denfromufa, I've updated the link.

@matinfo

matinfo commented Mar 20, 2017

Copy link
Copy Markdown

Good to know, to not use default Python of OSX Sierra:

Add to your shell (~/.bashrc or ~/.zshrc) :

export PATH="/Users/username/.pyenv:$PATH"
eval "$(pyenv init -)"

Remplace username by your MacBook user.
Refresh your terminal env, example: $ source ~/.zshrc (or restart your terminal)

@yanpeng

yanpeng commented Jun 8, 2017

Copy link
Copy Markdown

@matinfo it saves me :-)

@devhero

devhero commented Jun 12, 2017

Copy link
Copy Markdown

@matinfo

Can also works using shims (pyenv doc):

export PATH="/shims:$PATH"
eval "$(pyenv init -)"

@gino8080

gino8080 commented Dec 3, 2017

Copy link
Copy Markdown

great thankyou!

@yammyxing

Copy link
Copy Markdown

Nice work~

@nachozt

nachozt commented Mar 8, 2018

Copy link
Copy Markdown

Thanks!!

@adlersantos

Copy link
Copy Markdown

Thank you guys!

@danishbacker

Copy link
Copy Markdown

Thank you 👍

@cindyloo

Copy link
Copy Markdown

yes, very helpful!

@crapthings

Copy link
Copy Markdown

pyenv install 3.7.2
python-build: use openssl from homebrew
python-build: use readline from homebrew
Downloading Python-3.7.2.tar.xz...
-> https://www.python.org/ftp/python/3.7.2/Python-3.7.2.tar.xz
Installing Python-3.7.2...
python-build: use readline from homebrew

BUILD FAILED (OS X 10.14.3 using python-build 20180424)

Inspect or clean up the working tree at /var/folders/g_/9zn58k_16m50w1z3tqvq65xr0000gn/T/python-build.20190214115852.93619
Results logged to /var/folders/g_/9zn58k_16m50w1z3tqvq65xr0000gn/T/python-build.20190214115852.93619.log

Last 10 log lines:
File "/private/var/folders/g_/9zn58k_16m50w1z3tqvq65xr0000gn/T/python-build.20190214115852.93619/Python-3.7.2/Lib/ensurepip/main.py", line 5, in
sys.exit(ensurepip.main())
File "/private/var/folders/g
/9zn58k_16m50w1z3tqvq65xr0000gn/T/python-build.20190214115852.93619/Python-3.7.2/Lib/ensurepip/init.py", line 204, in main
default_pip=args.default_pip,
File "/private/var/folders/g
/9zn58k_16m50w1z3tqvq65xr0000gn/T/python-build.20190214115852.93619/Python-3.7.2/Lib/ensurepip/init.py", line 117, in _bootstrap
return _run_pip(args + [p[0] for p in PROJECTS], additional_paths)
File "/private/var/folders/g
/9zn58k_16m50w1z3tqvq65xr0000gn/T/python-build.20190214115852.93619/Python-3.7.2/Lib/ensurepip/init.py", line 27, in _run_pip
import pip._internal
zipimport.ZipImportError: can't decompress data; zlib not available
make: *** [install] Error 1

@LongBu

LongBu commented Feb 20, 2019

Copy link
Copy Markdown

@crapthings

Try the following:

CFLAGS="-I$(brew --prefix readline)/include -I$(brew --prefix openssl)/include -I$(xcrun --show-sdk-path)/usr/include" LDFLAGS="-L$(brew --prefix readline)/lib -L$(brew --prefix openssl)/lib" PYTHON_CONFIGURE_OPTS=--enable-unicode=ucs2 pyenv install -v 3.7.2

reference:
pyenv/pyenv#1219

@easyas314

Copy link
Copy Markdown

Yikes! I had the same error as @crapthings and the arcane incantation from @LongBu made my screen puke all fun kinds of output and eventually ended with:

Collecting setuptools
Collecting pip
Installing collected packages: setuptools, pip
Successfully installed pip-18.1 setuptools-40.6.2
/var/folders/rz/fjd3x9jn24n8kxhybt17yh_40000gn/T/python-build.20190224164731.13157 ~
~
Installed Python-3.7.2 to /Volumes/DATA_MAC/brad/.pyenv/versions/3.7.2

... and pyenv versions lists 3.7.2
Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment