I spent the best part of this morning mucking around with Python configs, so I've outlined the best practice way of developing in Python on a Mac. You should do this if you don't want to run into troubles down the line with your development environments.

Why this setup?


Sure, you could run the bare Apple-supplied version of Python, however keep in mind:

  • it's often a few versions behind the current version (so you'll be stuck with an older version of OpenSSL that won't work with PayPal, like I was this morning)
  • it's custom built and uses funny values so won't always work with libraries like matlab
  • you can't control, sandbox and replicate your developer environment to say, a production server, or someone else's machine


So it's better to just use the developer builds that you can grab from something like Brew, and use it with a virtual environment (virtualenv in Python land), which you can control and replicate across developer environments.

Additional config that I had to do to get this working can be found in the bonus section, although I haven't found an adequate explanation for why this is needed. Only follow it if you have the same issues I have.

  1. Install pip on your machine
  2. Install virtualenv on your machine
  3. Install the latest version of Python via brew
$ brew install python

4.  Create a virtual environment and use the brew version for your interpreter

$ virtualenv --python
  [path_to_your_brew_python_interpreter_here]
  [virtual_environment_folder_here]

5. Use the environment and be a happy and healthy Python developer!

Further reading


Homebrew & Python - a good read to get your head around the relationship

Virtualenv - important to read before you do anything with virtualenv; I spent ages being scared about virtualenv but once I read the docs it all made sense

Bonus: Fixing Up VirtualEnv + Brew

After installing Python via brew, I had trouble setting up my virtual environments to use the brew executable. I kept getting this error (abbreviated for brevity's sake):

IOError: [Errno 2] No such file or directory:
    '/usr/local/opt/python/bin/python2.7'

I still don't quite understand why this was happening, although someone else had a similar problem and managed to solve it.

Long story short: their solution didn't work for me, so I just created an alias between /usr/local/opt/python/bin (needing to create the python/bin directory, since it didn't exist) and the system version of Python.