Skip to content

Instantly share code, notes, and snippets.

@uraimo
Last active February 5, 2019 16:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uraimo/b76a4450640e24efdb0c54df4955c264 to your computer and use it in GitHub Desktop.
Save uraimo/b76a4450640e24efdb0c54df4955c264 to your computer and use it in GitHub Desktop.
Installing Python 3.6.5 on macOS Mojave for Tensorflow

Since Tensorflow is not yet available via pip for Python 3.7 and pyenv can't build python on the latest macOS, let's install 3.6.5_1 via homebrew and use it in conjunction with pyenv to create an enclosed environment that will use 3.6 only when we need it:

brew unlink python
brew install --ignore-dependencies https://raw.githubusercontent.com/Homebrew/homebrew-core/f2a764ef944b1080be64bd88dca9a1d80130c558/Formula/python.rb
brew switch python 3.7.2_1

Install pyenv if you haven't already, we'll use it to switch to 3.6.5 only when we need it (e.g. jupyter), leaving the latest Python from homebrew as default release (e.g. 3.7.2_1):

brew install pyenv
pyenv rehash 

And then add eval "$(pyenv init -)" to your .bash_profile

Add links to homebrew's pythons to pyenv:

ln -s /usr/local/Cellar/python3/3.6.5_1 ~/.pyenv/versions/3.6.5_1
ln -s /usr/local/Cellar/python3/3.7.2_1 ~/.pyenv/versions/3.7.2_1

And now fix the symbolic links to all the python 3.6 binaries (homebrew doesn't create them since 3.6 is not the currently selected version, we need to do it manually):

ln -s /usr/local/Cellar/python3/3.6.5_1/Frameworks/Python.framework/Versions/3.6/bin/2to3-3.6 /usr/local/opt/python/bin/2to3-3.6
ln -s /usr/local/Cellar/python3/3.6.5_1/Frameworks/Python.framework/Versions/3.6/bin/idle3.6 /usr/local/opt/python/bin/idle3.6
ln -s /usr/local/Cellar/python3/3.6.5_1/Frameworks/Python.framework/Versions/3.6/bin/pydoc3.6 /usr/local/opt/python/bin/pydoc3.6
ln -s /usr/local/Cellar/python3/3.6.5_1/Frameworks/Python.framework/Versions/3.6/bin/python3.6 /usr/local/opt/python/bin/python3.6
ln -s /usr/local/Cellar/python3/3.6.5_1/Frameworks/Python.framework/Versions/3.6/bin/python3.6-config /usr/local/opt/python/bin/python3.6-config
ln -s /usr/local/Cellar/python3/3.6.5_1/Frameworks/Python.framework/Versions/3.6/bin/python3.6m /usr/local/opt/python/bin/python3.6m
ln -s /usr/local/Cellar/python3/3.6.5_1/Frameworks/Python.framework/Versions/3.6/bin/python3.6m-config /usr/local/opt/python/bin/python3.6m-config
ln -s /usr/local/Cellar/python3/3.6.5_1/Frameworks/Python.framework/Versions/3.6/bin/pyvenv-3.6 /usr/local/opt/python/bin/pyvenv-3.6 

Now you are all set. Let's suppose you plan to run a jupyter notebook/lab using tensorflow, the first thing you need to do is set a local python release, running from the directory containing the notebooks (more practical this way):

pyenv local 3.6.5_1

Then just install the packages you need with pip3 and launch jupyter from this directory and it will be executed using python 3.6.5.

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