Python3 Setup on OSX/MacOS High Sierra

By default MacOS has Python 2.7 installed out of the box, however for my PhD project I am using a number of libraries that require Python 3.x.x. There are numerous approaches to deploy multiple versions of Python to MacOS, so I’ve documented the approach I took.

A big thanks to Chris Mendez, as my approach is based on his article: Installing Multiple versions of Python on Mac using Home-brew

I already had Homebrew installed, but incase you haven’t there is a tutorial on Installing Homebrew for Mac. I’ve tried a number of approaches using VirtualEnv and found that PyEnv seems to do the job much better. The steps as follows:

brew install pyenv
echo 'eval "$(pyenv init -)"' >> ~/.bash_profile
. ~/.bash_profile

I then looked for the lastest version of python (version 3.6.5) and installed it using PyEnv, and switched the local and global environmenal settings to point to it.

pyenv install 3.6.5

The next problem was to install all the modules currently loaded on the MacOS provided version of Python (version 2.7.10) the local and global environmenal settings to point to it. So before switching to the new version, I used 'pip' to extract the list to a file, '/tmp/2.7-module.list’. I then edited the file, to remove any version 2 specific modules, also I removed 'MySQL-python’ as it has been replaced with a nice shiny pure python MySQL client library ‘PyMySQL’, and I’d had problems with it.

pip list > /tmp/2.7-module.list
- edit the file to remove unwanted modules -
pyenv global 3.6.5

for l in `cat /tmp/2.7-modules.list | awk '{ print $1 }'`;do
 pip install $l;
done

Make note of which modules were installed sucessfuly and if there are any errors.

Just a quick note on ‘PyMySQL’. I have installed both client and server on my Mac, so I did’t experence library dependencies. The reason was that I'd previously had great fun trying to install ‘MySQL-python’ without MySQL server installed locally, and in the end gave and installed it. For this reason I’m guessing I didn’t have any problems installing ‘PyMySQL’.

I ran into a problem and originaly needed to use 'sudo pip' to install modules for the system version of Python, as the libriaries were installed in the system library. This is no longer required with PyEnv as all the libraries as installed under ‘~/.pyenv/‘ tree.

Quick reference:

pyenv versions         - lists the versions under its control
pyenv global system - switches back to the system version (2.7.10 for my system)
pyenv global 3.6.5    - switches to version 3.6.5


A big thanks to the authors of the software I’ve used. 

Python    - https://www.python.org/
PyEnv     - https://github.com/pyenv/pyenv
PyMySQL - https://github.com/PyMySQL/PyMySQL

© Mick Vaites 2018