Sublime Text 2 Python Development Environment in Windows Guide
using Sublime Text 2 + distribute + pip + nose + coverage + SublimeLinter + SublimePythonCoverage
This guide will show you how to setup a great development environment for Python using Sublime Text 2 on Windows. If you’re new to Python or looking to move to Sublime Text 2, this guide is for you. If you’ve completed parts of this guide on your own, please confirm your settings by going to the sections marked “Test” first before moving on. Now, let’s take a look at the tools we will need:
Python 3 - We need to develop in Python 3 to avoid future technical debt from upgrading from Python 2.
Text Editor - We need a fully robust text editor with plugins, builds, syntax highlighting, regex matching, multiple views, and text editing prowess. For this, we are using Sublime Text 2. It’s a great text editor that people are falling in love with and are using as an alternative to VIM / Emacs.
Package Tools - We need modern package tools so we can install new software, plugins, and tools at the drop of a hat. We will use Distribute + pip for this.
Lint - We need a smart lint that will allow us to write clean, readable code. Even though PyLint is more robust, we will use SublimeLinter for compatibility reasons.
Unit Testing - We need a tool that can offer automated unit testing of our software and equip us with robust tools for writing tests. For this, we will use nose because it gives us an incredible about of power over the built in PyUnit. In addition, nose has a plugin architechure that allows us to install plugins for it or write our own. With this we can extend our testing prowess further.
Code Coverage - We need a tool to measure our code coverage so we can write more accurate and comprehensive unit tests. For this, we will use coverage.py.
These are the basic tools any python programmer should have at her disposal. This guide intentionally leaves out a few types of tools, including:
Debuggers - In practice, many python programmers prefer to interact with errors directly than run a debugger. If you’re still looking to debug, you can use Python’s built in pdb debugger module. Sublime Text 2 has several plugins to support pdb debugging.
IDEs - Python is a lightweight language that can do very powerful things and many would argue that it feels wrong to use a heavyweight IDE (like Eclipse / PyDev) to run Python code. Instead, we’ve chosen to use Sublime Text 2, which gives us a much more modular approach to setting up our development environment.
Profilers - Python includes a standard library module called profile that can spit out performance statistics for you. This can have some uses, but in practice we are more likely to use our unit tests to drive performance testing. Nose has many tools that allow you to do this, and even more powerful plugins are available.
Other tools - Optimizers, Documentation, Packaging, and more. There’s a whole world of Python out there beyond just writing and testing code that I didn’t include here.
Installing Python 3 On Windows (or “Some Assembly Required”)
Head on over to http://www.python.org/getit/ and download and install the latest Windows x86-64 msi installer. (At the time of this writing, Python 3.3.0 is the latest.) This will install python on your system.
Test: Open a new command prompt (win + cmd + enter). Run the python command.
Test Results: Oh look, an error! Why did that happen?
Setting Environment Variables (or “How to Age 5 Years in 5 Minutes”)
To get python working correctly, we need to set our environment variables so the command prompt knows where to find python. Open environment variables by going to Control Panel\All Control Panel Items\System Click on the Advanced system settings link on the left, then the Environment Variables… button.
We’re going to set up an alias for Python’s system variable so we can reuse it. For the System Variables click the New… button. Enter a variable name of PYTHON_HOME and a value of C:\Python32 (or where-ever you installed Python previously in this guide. The default location is C:\Python + the first two numbers of the version you installed). Click OK.
Now edit the System Variable - Path. At the end of the path, add the following two entries:
The first environment variable sets our python path (so when we run the python command, cmd finds it.)
The second one sets the path where python packages will get installed. Environment variables are semicolon delimited, so make sure this is right!
Finally, edit the User Variable - Path. Do the same thing - at the end of the path, add the following two entries:
Some Sublime Text 2 plugins will use your User Variable - Path explicitly, so make sure to set it too.
Test: Open a new command prompt (win + cmd + enter). Type echo %python_home% + enter. Type python + enter.
Test Results: echo %python_home% will return your Python variable (C:\Python32 for me). Python will return your version of Python and will start an interactive Python session. (Type ctrl + z + enter to exit the interactive session.)
Installing distribute and pip On Windows (or “The Two Gatekeepers”)
We’re almost to a beautiful world where installing new things is just a command away. First, we need distribute and pip. Many developers are favoring these over setuptools and easy_install, especially python 3 developers.
But wait! These are in .tar.gz files! What the heck are those?
No worries. You can use 7zip to unzip these files. .tar files (or “tarballs”) and .gz (or “gzips”) is just another packaging tool like .zip or .rar.
Unpackage your files and open up a command prompt. You can install them by simply navigating to their directory (cd c:\path\to\directory) and typing python setup.py install.
Test: Run the pip command in a command prompt. Check the C:\Python32\Lib\site-packages directory for the distribute folder.
Test Results: pip will return an error in the command prompt. The distribute folder will exist.
Installing nose and coverage On Windows (or “How I Learned To Stop Worrying and Love The pip”)
Open a command prompt.
Run the following commands:
pip install coverage
pip install nose
It’s that easy! Now you have both code coverage and unit testing tools installed.
Test: Open a command prompt. Run the nosetests command and the coverage command.
Test Results: nosetests and coverage will return error messages.
Installing Sublime Package Control (or “The Day I Fell In Love With Sublime Text Plugins”)
Head over to the Sublime Package Control website and follow their instructions for installing Sublime Package Control.
I’ve also reproduced the installation instructions here, but note that these may go out of date.
To install Sublime Package Control, open Sublime Text 2 and open the command prompt (ctrl + `) (That’s the `/~ key, not the ‘/” key!). Paste the following text:
import urllib2,os; pf=’Package Control.sublime-package’; ipp=sublime.installed_packages_path(); os.makedirs(ipp) if not os.path.exists(ipp) else None; urllib2.install_opener(urllib2.build_opener(urllib2.ProxyHandler())); open(os.path.join(ipp,pf),’wb’).write(urllib2.urlopen(‘http://sublime.wbond.net/’+pf.replace(’ ‘,’%20’)).read()); print ‘Please restart Sublime Text to finish installation’
Restart to finish the installation.
Test: Open Sublime Text 2. Type ctrl + shift + p and type “Package”.
Test Results: If you see the Package Control: settings, you’ve successfully installed it.
Installing SublimeLinter and SublimePythonCoverage (or “It’s As Easy As Control + Shift + P!”)
Now we need to install our Lint (which will help us identify code style errors) and install a plugin so our unit tests and coverage will work.
To install both, simply type ctrl + shift + p and use the Package Control: Install Package command.
Search for SublimeLinter and SublimePythonCoverage and press enter to install them.
When you’re done, make sure to go to Tools > Build System > Python Nose with Coverage. This will set your build system to automatically run unit tests using nosetests and coverage.
Test: Open myprint.py in Sublime Text 2. Build myprint.py (ctrl + b)
Test Result: If you built myprint.py and received coverage information, then SublimePythonCoverage is working correctly!
Now your dreams of a robust development environment on Windows can come true. :)