Hacker Codex
Hacker Codex
Hacker Codex
Python Development
Environment on macOS Sierra
and El Capitan
Last updated: September 28, 2017
First steps
This guide assumes that you have already installed
Homebrew. For details, please follow the steps in the
macOS Configuration Guide.
Python
We are going to install the latest 2.7.x version of Python
via Homebrew. Why bother, you ask, when Apple includes
Python along with macOS? Here are some reasons:
https://hackercodex.com/guide/python-development-environment-on-mac-osx/ 1/8
22/12/2017 Hacker Codex
Pip
Let’s say you want to install a Python package, such as the
fantastic Virtualenv environment isolation tool. While
nearly every Python-related article for macOS tells the
reader to install it via sudo pip install virtualenv ,
the downsides of this method include:
https://hackercodex.com/guide/python-development-environment-on-mac-osx/ 2/8
22/12/2017 Hacker Codex
Version control (optional)
The first thing I pip-install is Mercurial, since I have
Mercurial repositories that I push to both Bitbucket and
GitHub. If you don’t want to install Mercurial, you can
skip ahead to the next section.
vim ~/.hgrc
[ui]
username = YOUR NAME <[email protected]>
hg debuginstall
https://hackercodex.com/guide/python-development-environment-on-mac-osx/ 3/8
22/12/2017 Hacker Codex
Virtualenv
Python packages installed via the steps above are global in
the sense that they are available across all of your
projects. That can be convenient at times, but it can also
create problems. For example, sometimes one project
needs the latest version of Django, while another project
needs an older Django version to retain compatibility with
a critical third-party extension. This is one of many use
cases that Virtualenv was designed to solve. On my
systems, only a handful of general-purpose Python
packages (such as Mercurial and Virtualenv are globally
available — every other package is confined to
virtual environments.
[install]
require-virtualenv = true
[uninstall]
require-virtualenv = true
https://hackercodex.com/guide/python-development-environment-on-mac-osx/ 4/8
22/12/2017 Hacker Codex
cd ~/Virtualenvs
virtualenv foobar
If you have both Python 2.x and 3.x and want to create a
Python 3.x virtualenv:
https://hackercodex.com/guide/python-development-environment-on-mac-osx/ 5/8
22/12/2017 Hacker Codex
gpip(){
PIP_REQUIRE_VIRTUALENV="" pip3 "$@"
}
(As usual, after adding the above you must run source
~/.bash_profile for the change to take effect.)
Creating virtual environments
Let’s create a virtual environment for Pelican, a Python-
based static site generator:
cd ~/Virtualenvs
virtualenv pelican
cd pelican
source bin/activate
https://hackercodex.com/guide/python-development-environment-on-mac-osx/ 6/8
22/12/2017 Hacker Codex
Dotfiles
These are obviously just the basic steps to getting a
Python development environment configured. Feel free to
also check out my dotfiles (GitHub mirror).
https://hackercodex.com/guide/python-development-environment-on-mac-osx/ 7/8
22/12/2017 Hacker Codex
https://hackercodex.com/guide/python-development-environment-on-mac-osx/ 8/8