A collection of things I’ve learned that set up a stable and easy to use environment for writing Python code on a Mac.
- Install Command Line Tools for X Code (in order to install Homebrew)
You’ll need an Apple ID for this.
- Install Homebrew
- Install a text editor, I use (and recommend) Sublime Text 3
If you like it, you should buy a licence too :)
- Setup Sublime Text command line tool
From terminal run:
ln -s “/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl” /usr/local/bin/subl
Sublime docs recommend creating the symlink in
~/bin/but I went with
- Install Package Control for Sublime Text
Install instructions. Package Control enables the installation of thousands of useful plugins.
- Install Git for version controlling your code
brew install git
$PATHlooks like mine (
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin), this should be all you need to do to get the brewed verison of Git, rather than the default Mac OS version. You might need to restart Terminal to make the brewed version active.
- Customise Sublime Text for Python
There is a lot you can do — I recommend Dan Bader’s book.
- Install Python via Homebrew
brew install python3
$PATHlooks like mine (
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin), this should be all you need to do to get the brewed verison of Python, rather than the default Mac OS version.
- Install virtualenv and virtualenvwrapper for managing Python environments
For a quick guide see: http://docs.python-guide.org/en/latest/dev/virtualenvs/
As the complexity of code grows virtual environments are great for managing dependencies etc. I find virtualenvwrapper much easier to use than the straight virtualenv.
- Install iPython and Jupyter Notebooks
pip3 install jupyter
I haven’t used iPython for much other than in interactive Python shell. It can do lots more, but even the shell is a great improvement on the default Python interactive shell.
Jupyter notebooks allow you to write, run, and even annotate Python code directly in your browser, and do all sorts of nice stuff after — like share your code with others. Installing Jupyter also installs iPython.
And that’s about it. This will get you up and running with writing Python code. Doing this, and then keeping things up to date and maintained, should mean you don’t have too many issues when writing.