+16

Run Python script in a virtual environment (virtualenv)

Rob Speer 7 years ago • updated by Jonatas Castório Damasceno 5 years ago 6
The "Build" command for a Python script runs it through the global Python environment, but all my real code lives in virtual environments.

It would be helpful if Sublime Text had a way to choose a Python environment.
+2
Then "Build with arguments..." and "Build with last arguments" come into my mind.
Could someone explain to me how to set up a build system to use a virtualenv'd python. For example I always make a folder called 'env' within my project directory. How do I build to use that python and not the system python? (on osx) Stuck with this
+1
This is a build system improvement, not a ST2. I think, better to move this functionality into separated plugin(i believe Jon read this message +_+) and give it to community.
But now, best solution - add arguments using "Build with arguments". And nothing need to add in ST2.
I have a working solution which I have been using for some days now (OSX, virtualenv per project or repo). It should work within Linux, too. Read the details here:
http://www.sublimetext.com/forum/viewtopic.php?f=5&t=1699
You already can run any build using a virtual env. You just need to call the bin from that virtual env. For exemple, to run unit test you can do:

{
    "cmd": ["/home/you/.virtualenvs/env_name/bin/unit2", "discover"],
    "working_dir": "${project_path:${folder}}"
}

Now the biggest problem is more that you can't easily create and switch build systems. You have to go menu, then submenu, then click. Then go menu again. Then run.

A litlle late, but here is a solution that I used - I`ve set as build_systems inside sublime project configs.

The example.sublime-project looks like:


{

"folders":

[

{

"path": "/path/to/your/project"

}

],

    "build_systems":

    [

        {

            "name": "Run Tests",

            // activate the specific virtualenv for the project

            "cmd": ["source", "/path/to/your/virtualenv/bin/activate"],

            // now indicates a different folder to run the next cmd

            "working_dir": "/path/to/to/you/django_project",

            // runs the test

            "cmd": ["python", "manage.py", "test"]

        }

    ]

}