Ludovic's weblog

virtualenv + SublimeText

Integrating virtualenv in SublimeText's build system

When programming in python, it's a good practice to use virtualenv to clearly separate the dependencies between different projects. However, it's not always straightforward to integrate virtualenv within your current workflow.

I like to code using SublimeText2 because it fits my needs to have both a powerful code editor, and a simple text editor to write markdown, but hitting ctrl + shift + b while editing a python file will execute it using the python install that comes first in your $PATH environment variable, which is obviously not what you want.

A good way to integrate virtualenv with a SublimeText2 is to create a project file that contains a custom build system that will run your scripts using your virtualenv's python.

{
  "folders":
  [
    {
      "name": "Project",
      "path": "."
    }
  ],
  "build_systems":
  [
    {
      "name": "Python virtualenv",
      "selector": "source.python",
      "cmd": ["$project_path/env/bin/python", "-u", "$file"]
    }
  ]
}

That's it.

Your script will now be executed with whatever python version you used to create your virtual environment.