Pythonity Blog Pythonity Blog

Python Developer Environment on OS X

I recently decided that my beloved ThinkPad x220 - the last of its kind before Lenovo started slowly destroying (changing? nah, let's go with destroying) the famous IBM computer line - was slowly getting too old. Don't get me wrong, it's an awesome computer, with perfect size, beautiful IPS screen (seriously, why are they so rare?), the best keyboard I ever had and TrackPoint that I will miss very much. And did I mention the built-in 3G module, docking station, and the ability to dismantle the whole thing with one screwdriver and half an hour of free time? Wait, what was I talking about?

Yeah, it was and still is an awesome computer. But 4 years is a lot, especially if it's also your work tool. All that came at the time when my brother was selling his old MacBook Pro which had me intrigued for a long time (after doing a bit of research and painfully removing ThinkPads from the consideration, the only real alternative to Apple was System76), so here we are.

After about a week of using it, I have a million thoughts, complaints, and pleasant surprises about OS X (or should I say "macOS"? ugh) and the laptop itself, but this is neither the place nor the time for those. Instead, I want to focus on setting up a Python developer environment on it.

As a long time Ubuntu and elementary OS user I kinda took apt-get and PPAs for granted. They're awesome. Need something? There's a good chance that it's already in the repositories or someone made a repository with it and hosted it (for free) on [Launchpad][launchpad], so it's available for anyone. So just do apt-get install planet-express and you're good to go, with automatic and centralized updates no less. Isn't that awesome? So... none of that on OS X.

Well, at least by default, because that's where the awesome Homebrew comes in. "The missing package manager for OS X" - rarely a description is so on point that I don't really have anything to add. Seriously, I don't think I would be able to use OS X without it. So install it if you haven't already:

/usr/bin/ruby -e "$(curl -fsSL"

Anyway, Python environment. The most basic setup would be brew install python python3, but I wouldn't really call that a Python developer environment. What if you need both Python 3.4 and 3.5? What about using tox and testing it under a number of Python versions? Virtual environments anyone? Homebrew doesn't really offer such flexibility (why would it? it's a package manager after all) and its maintainers agree - anything more complicated than using the latest version of Python 2 and/or 3 with it is a no no.

Luckily, pyenv comes to the rescue. It basically allows you to install any Python version released. You need Python 2.2.2 and 3.3.3 for some weird reason? Pyenv has your back:

$ brew install pyenv
$ pyenv install 3.5.2
$ pyenv install 3.4.5
$ pyenv install 3.3.3
$ pyenv install 2.7.12
$ pyenv install 2.2.2
$ pyenv versions
* system (set by /Users/bender/.pyenv/version)

You can then set these Python version(s) globally (rather self-explanatory) or locally (it saves passed Python versions in .python-version in current directory, so that it can 'set' them on entering it). Really cool stuff. But it gets even better.

Pyenv also has two plugins to manage so-called virtual environments (or just virtualenvs if you will) - pyenv-virtualenv and pyenv-virtualenvwrapper - both awesome looking, each with a slightly different approach. I previously used virtualenvwrapper and was tempted to do the same here. But after playing with both of them for a few minutes I opted in favor of pyenv-virtualenv, and I don't really have any complaints. Everything is straightforward and just works:

$ brew install pyenv-virtualenv
$ pyenv 3.3.3 flexo
$ pyenv virtualenvs
  3.3.3/envs/flexo (created from /home/bender/.pyenv/versions/3.3.3)
* flexo (created from /home/bender/.pyenv/versions/3.3.3)

The activation and deactivation also works as expected:

$ pyenv activate flexo
$ pyenv deactivate

I won't really go into the rest of my stack, but I can recommend the following:
- Homebrew Cask - Homebrew but for GUI apps
- Antigen - Awesome plugin manager for zsh, an alternative to oh-my-zsh
- iTerm 2 - Better terminal replacement
- Honukai Theme - Just a iTerm/zsh eyecandy, but what a eyecandy it is
- Sublime Text - Some prefer TextMate, I prefer Sublime Text
- PyCharm - My IDE of choice
- Dash - Extremely convenient offline documentation for programmers
- Tunnelblick - VPN client. Gets the job done

As always - feel free to ask any question.