Sublime Forum

How to activate virtualenv and run file from python via build?

#1

When I run it it just keeps saying it finishes in 0.0 seconds. What am I doing wrong?

No output in the build window. :frowning:

Running on Mac.

{
	"shell":true,
	"cmd": ["cd", "/Users/casey/virtualenvs/site-website/bin", "&", "activate", "&", "python", "$file"],
	"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
	"selector": "source.python"
}

0 Likes

#2

I think is does not work because Sublime Text does not have a full terminal support.

You can up vote their feature requests on:

  1. https://github.com/SublimeTextIssues/Core/issues/1468 Full/complete terminal features on Sublime Text
  2. https://github.com/SublimeTextIssues/Core/issues/478 Allow for user interaction when running programs in the Build Panel

You can try to call a shell script as:

{
    "shell":true,
    "cmd": ["sh","$packages/User/MyShellBuilder.sh","$file"],
    "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
    "selector": "source.python"
}

And create the file MyShellBuild.sh on your Sublime Text User folder:

cd /Users/casey/virtualenvs/site-website/bin & activate & python $1

This one I used on:

  1. https://github.com/evandrocoan/SublimeAMXX_Editor/blob/master/AmxxPawn.sublime-build

If it still not working within Sublime Text, you can make Sublime Text launch a full terminal window and then run the shell script. If you have the xfce4-terminal terminal installed, your can change you shell script as:

/usr/bin/xfce4-terminal --maximize --hold --command="cd /Users/casey/virtualenvs/site-website/bin & activate & python $1"

This one I used on:

  1. https://github.com/evandrocoan/ComputerScienceGraduation/blob/master/OperatingSystems/OperatingSystems20162.sublime-project
0 Likes

#3

A potential issue with this is that command1 && command2 means ā€œrun command1, and if it succeeds, run command 2ā€, whereas command1 & command2 mean ā€œrun command1 in the background, and while that’s happening, run command 2ā€.

Presuming that a chained command line such as the one you’re using does what you want from within the terminal (I have no experience with virtual environments), a potential fix for your issue would be to replace all of the "&" with "&&" instead and see if that works better for you.

1 Like

#4

@OdatNurd tried that and it didn’t work.

@addons_zz I’ll give this a go as the logic looks good, didn’t know that sublime didn’t have full terminal support.

1 Like