< More articles

Using Sublime Text for python

October 10, 2016

Five or so years ago, I was frustrated by my coding environment. I was working on .net web sites and felt like I was fighting Microsoft’s Visual Studio to get my work done. I started jumping between Visual Studio, Notepad++, Eclipse, and other programs. With all my jumping around, I was not really happy with my experience in any of them.

I came across an article that encouraged me to invest in my coding tools; that is to pick one program and dive deep into it, find out what it’s good for, and try to enjoy it to its maximum, before looking to other tools.

Given that inspiration, I turned to Sublime Text and the myriad of how-to articles and videos for it, and within a month it was my favorite editor. I could list dozens of reasons why you should give it a try, but Daniel Bader has done a great job.

One great feature not listed in Daniel’s article is how Sublime supports so many languages. It is as flexible as your interests. As you prepare to try Sublime for python development, I would suggest adding these packages to increase your enjoyment:

Anaconda

The Anaconda package gives you a number of helpful tools for python development. Probably the one I use most often is its auto-completion function that enhances Sublime’s auto-completion window with greater intelligence.

From helping import modules, to calling their functions, providing parameter options, and popping up function documentation, it can be a big help right when you need it.

Also useful its its enhanced `Goto definition` functionality, which does what you expect, and does a better job routing you to the correct python function than Sublime’s native functionality. I find this particularly useful when working with a new open source module.

The third thing I find incredibly useful is it’s linting property. When you finish typing, it’ll lint your code against PEP8

Now, when you install anaconda, remember to add an additional configuration to your `.sublime-project` files. In order for anaconda to provide its help to the unique packages in each project, you will need to add a “python_interpreter” key and value to your project settings. For example:

{
  "folders":
    [
      {
        "path": "."
      }
    ],
  "settings":
    {
      "python_interpreter": "~/.virtualenvs/envname/bin/python3.5"
    }
}

I know there are more things anaconda can do, but I haven’t gotten to them yet. These have been by far worth it.

SublimeREPL

The SublimeREPL package is fantastic to have around, as it allows you to trivially spawn a REPL environment for pretty much any language you have installed on your machine. In particular, I’m thankful that it has excellent support for virtualenv environments. So that all I need to do to check some thoughts is hit:

  • `shift-command-p` For the command pallet
  • vir” To select the SublimeREPL: python - virtualenv command
  • Select which virtual environment I want to activate

And viola! I’m ready to go!

SublimeREPL also allows you to quickly send code over to your REPL with quick commands. So, if you want to have all your variables and functions in your new REPL, create one and hit Tools > SublimeREPL > Eval in REPL > File, or as I commonly do hit Control-comma then f. To send a few lines you have selected, it’s Control-comma then l.

Unfortunately, at the moment SublimeREPL does not support virtual environments that are in the project folder, like the ones created with python 3.3+ pyvenv tool. So for now, if you want to take advantage of this functionality, keep using the virtualenv tool.

Origami

This is not a python-specific tool, but it helps out in so many ways. Origami makes it easy to move or clone windows into new or existing panes.

So for example, whenever I spawn a new REPL to test some ideas, I find it helpful to split my Sublime window into two panes, with the code on the left and the REPL on the right. Also, I’m usually creating a REPL when I only have one pane. So instead of going through the menus and dragging the REPL over to a new pane, I hit the three commands listed above to create the REPL, and then hit command k then command option right arrow for the Origami: Create Pane with File on the Right command.

I know that sounds like a crazy bit of finger dancing, but after a few minutes with the origami command it makes sense. command k is one of the common Sublime key commands to say “Pay attention Sublime, I’m going to do something”. After that, the next command tells origami what to do. After a couple of times of using the command pallet to do your most common command you’ll start to remember what it is.

< Check out other articles