26

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

Rigobert Song
  • 2,766
  • 2
  • 30
  • 47

15 Answers15

15

You can also set the path for the build system to the bin directory of your virtualenv, like so:

"build_systems":
[
    {
        "selector": "source.python",
        "env": {"PYTHONPATH":"/Users/user/project"},
        "path":"/Users/user/work/myvirtualenv/bin:$PATH",
        "name": "Run virtualenv python",
        "cmd": ["python", "-u", "$file"],
        "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",

        "variants": [
            {   "name": "Run all Tests",
                "working_dir": "/Users/user/project",
                "cmd": ["nosetests"]
            } 
        ]
    }
]

This also allows other tools, like nose in the example, to find the correct python binary from the virtualenv.

user1248490
  • 963
  • 9
  • 16
  • Just gave you +1 but want to point out that "name" is only valid inside a variant. Refer to: [sublime-text-2-docs][st2] [st2]: http://sublime-text-unofficial-documentation.readthedocs.org/en/sublime-text-2/reference/build_systems.html – Randy Skretka Dec 01 '13 at 23:29
  • @user: Where do I need to add this file in ST? – user1050619 Feb 04 '16 at 19:50
  • @user1050619: from the [st 3 docs](http://docs.sublimetext.info/en/latest/file_processing/build_systems.html): *Build systems must be located somewhere under the Packages folder (e.g. Packages/User). Many packages include their own build systems.* The Packages folder lives in `~/Library/Application Support/Sublime Text 3/` with ST 3 on my Mac. – user1248490 Feb 05 '16 at 12:22
13

In windows this works for me:

"build_systems":
[
    {
    "name": "Run Tests",
    "working_dir": "/path/to/to/your/django_project",
    "cmd": ["/path/to/your/virtualenv/bin/python.exe", "manage.py", "test"]
    }
]
athspk
  • 6,722
  • 7
  • 37
  • 51
mak
  • 131
  • 4
  • Note @nebffa's answer below which reminds users to then activate the new build system by selecting it under Tools > Build System! – thumbtackthief Mar 04 '15 at 03:26
12

Sublime's Build System supports variables which can be used with Sublime project files to make this a bit more portable across projects.

If your virtual environments are in a standard spot, create a new project file (Project -> Save Project As) into the root directory of your project just above your virtual environment directory. Then create a new build file with something like this:

{

    "cmd": ["$project_path/venv/bin/python", "-u", "$file"]

}

It seems to then pick up the rest automatically - the same as if you typed ./venv/bin/python from the command line - no need to mess with paths, environment variables, etc.

Jens Erat
  • 37,523
  • 16
  • 80
  • 96
Russell Beattie
  • 326
  • 2
  • 5
3

I'm using Flask, but I think it's apply to nearly every case.
My actual build is like this, where "benicio" is the directory of my project:

{
    "cmd": ["source ~/projs/benicio/venv/bin/activate && python ~/projs/benicio/benicio_tests.py"],
    "shell": true
}
Dirtycoder
  • 125
  • 1
  • 7
3

Sorry to add yet another answer to this - but this caused me a large amount of grief figuring this out.

Not only do you need to make a build system like:

"build_systems":
[
{
    "name": "Maths",
    "env": {"PYTHONPATH":"/home/nebffa/Desktop"},
    "path":"$project_path/bin",
    "cmd": ["$project_path/bin/python3.3", "-u", "$file"]
}
]

but you HAVE to change a setting in Sublime Text - go to Tools --> Build System --> "Maths". In my case I need to choose "Maths" because that's what I named my build system. If you don't do this - Sublime Text does not use your build system!!

nebffa
  • 1,529
  • 1
  • 16
  • 26
2

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

Randy Skretka
  • 3,488
  • 3
  • 22
  • 14
  • 1
    This is the only solution that works for me as well, running ST3 + Ubuntu 13.10, with virtualenvs tucked away in my homefolder – puredevotion Apr 13 '14 at 15:56
1

Under MAC OSX, this works for me

{
"cmd": ["/your/virtualenv/bin/python", "-u", "$file"]
}

What i did was keep it simple:

Went to root drive and created python folder:

sudo mkdir python

then went in there and created the virtualenv

virtualenv --no-site-packages virtualenvname

then created a newbuild in ST2 with the above command and it works

sojim
  • 492
  • 2
  • 6
  • 19
1

This is what I have as a build system (assuming my virtualenv is created as a folder called 'env' in my current project directory). This at least means I don't need to constantly change the build system between projects:

{
   "cmd": ["env/bin/python", "-u", "$file"]
}

I saved this as a New Build System (Tools -> Build System -> New Build System).

HHHH
  • 1,197
  • 2
  • 16
  • 28
0

I use this to build my Flask project. I have added the following code to my Project Settings: Project -> Edit Project

{
     "folders":
     [
          {
               "path": "/C/MyDev/chilinzb/"
          }
     ],
     "build_systems":
    [
        {
            "name": "Flask",
            // activate the specific virtualenv for the project
            "cmd": ["C:/MyDev/virtualenvs/env_chilinzb/Scripts/python", "$file"]
        }
    ]
}

and then I just switch to my run.py file and hit Ctrl+B

happygoat
  • 791
  • 2
  • 7
  • 17
0

this combination worked great:2 steps

1) add the 2 appropriate keys to the 'env' key. A) DJANGO_SETTINGS_MODULE B) PYTHONPATH

2) update cmd to reflect the version of python you want to use.

{

"env":{"DJANGO_SETTINGS_MODULE":"my_project.settings",
    "PYTHONPATH":"d:\\Projects\\virts\\my_project\\Scripts;d:\\Projects\\virts\\my_project\\Lib;d:\\Projects\\virts\\my_project\\Lib\\site-packages;D:\\Projects\\my_project"
    },
"cmd": ["c:/Python27/python.exe","$file"]
}
davidj411
  • 985
  • 10
  • 10
0

I have an answer for anyone who uses Heroku and uses their foreman tool, and it works great. Simply create a new build system like so:

{
    "cmd": ["foreman", "run", "python", "-u", "$file"],
    "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
    "selector": "source.python"
}

This pulls in all of the environment variables available to Foreman, including your virtualenv's $PATH variable, which adds the virtualenv Python to your python path.

Dan Gayle
  • 2,277
  • 1
  • 24
  • 38
0

source did not work for me inside the build on lubuntu. use '.' or dot instead of 'source'.

this did work:

{

    "env": {
    "DJANGO_SETTINGS_MODULE":"django_project_name.settings",
    "PYTHONPATH":"/home/my_username/current/django_project_name:/home/my_username/.virtualenvs/django_project_name/lib/python2.7:/home/my_username/.virtualenvs/django_project_name/lib/python2.7/site-packages/"
},
"working_dir":"$project_path",
"cmd":[". /home/my_username/.virtualenvs/django_project_name/bin/activate && python $file"],
"shell":true

}

davidj411
  • 985
  • 10
  • 10
0

this worked for me:

{
    "cmd": ["C:/Users/user/virtualenvs/env1/Scripts/python.exe", "-u", "$file"],
    "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
    "selector": "source.python"
}

saved build in:

"C:\Users\user\AppData\Roaming\Sublime Text 2\Packages\User\"

as

"Python_env1.sublime-build"

Select

Tools> Build System> Python_env1

done!

using windows 8.1, st2

jes516
  • 542
  • 1
  • 13
  • 30
0

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.

Satendra
  • 6,755
  • 4
  • 26
  • 46
-4

Note comments about this solution being incorrect.

You have to edit your sublime-project file and add the following:

"build_systems":
[
    {
    "name": "Run Tests",
    "cmd": ["source", "/path/to/your/virtualenv/bin/activate"],
    "working_dir": "/path/to/to/you/django_project",
    "cmd": ["python", "manage.py", "test"]
    }
]
Charles Merriam
  • 19,908
  • 6
  • 73
  • 83
Romain
  • 6,322
  • 3
  • 35
  • 40
  • I doubt that it would work, a dictionary with 2 "cmd" entries. The second "cmd" will simply overwrite the first. – Virgil Dupras Aug 15 '12 at 17:07
  • But I think that if you remove the first "cmd" and replace "python" in the second "cmd" by a path to your virtualenv's python binary, it will work. IIRC, directly invoking a virtualenv's python works just as well as activating the virtualenv first. – Virgil Dupras Aug 15 '12 at 17:10
  • By changing the first `cmd` with `"env": {"PYTHONPATH": "/path/to/your/virtualenv/bin/activate"},` do you manage to get something ? – Romain Aug 15 '12 at 17:17
  • 3
    This does not work, and I cannot understand how this could have been marked as correct. The "source" command is a builtin command, not a binary, so it is not found in the PATH, and so it executes with "File not found error". Probably a mix of PYTHONPATH and evn would help, but I'm still trying to do that. – mhaligowski Mar 29 '13 at 11:17