Using virtualenv with sublime text 2

Viewed 19267

I am using sublime text 2 for python development along with virtualenv!

The standard sublime text 2 build system uses the standard python install rather than my virtualenv where my packages are installed.

How can I get sublime text 2 to build using my virtualenv?

I currently use the terminal to activate my environment and run my scripts.

UPDATE: Never got it working, but seeing as i am using flask and it builds when you make a change, it's not a big issue

15 Answers

I have just got sublime text 3 to working in a virtualenv. Although the OP specified ST2, there all likely more like myself who are using ST3. Thanks to user1248490 and Russell Beattie I arrived at the following:

{
    "shell_cmd": "$project_path/vi_sys_pkgs/bin/python3 -u \"$file\"",
    "path": "$project_path/vi_sys_pkgs/bin",
    "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
    "selector": "source.python"
}

Note that "cmd" is now "shell_cmd" in ST3. See ST3 blog

Assuming you keep your project-specific virtualenv in an .env-folder on the top level of your project.

  1. Sublime > Project > Save project as... (if you haven't already. This will allow you to set custom build options for the project
  2. Project > Edit Project:

    {
       "folders":[
           {
               "path": ".",
               "folder_exclude_patterns": [".env"],
           }
       ],
       "build_systems":[
           {
               "name": "Run in VirtualEnv",
               "shell_cmd": "source $project_path/.env/bin/activate && python -u $file"
           }
       ]
    }
    
  3. Tools > Build System > Run in VirtualEnv

  4. Tools > Build

Please note that this was tested with Sublime Text 3.

Related