Sublime Forum

Virtualenv

#1

Hi folks,

in the past days I’ve written a solution to run my code (e.g. test cases) thru my virtual environments under OSX. This involves several scripts:

  • exec.py: A wrapper to add support for wrapping for example the build commands to load a proper environment (necessary on OSX).
  • vrun.sh: Small wrapper to run vrun.py within a login shell.
  • vrun.py: Script to run a Python file thru a virual environment (virtualenv) if possible otherwise directly. Currently only supported within UNIX environments. Plz send me patches for Windows environmnts and I will implement it properly.

For this to work, I’ve set up a special folder ~/Code/env/ which contains all my virtual environments. I create an environment for each project I work on for some time in a separate folder (e.g. ~/Code/env/example_project/). I’m also using Mercurial and place every repo in ~/Code/hg/. After that I create a special project folder within ~/Code/ (e.g. ~/Code/example_project). Finally I symlink all my necessary repos into the project folder. When not using a project folder, I name my virtualenv folder like my repo folder so that they match by folder name.

vrun.py tries to use the CODE_ENV environment variable to find the envs therefore I’ve set it to ~/Code/env within my .bashrc.
After this vrun.py should already be able to launch a Python file within the desired environment. You can test it by running e.g. “vrun.py ~/Code/example_project/test_file.py” which then should print out a line beginning with an ampersand which shows what is being run. There you should see something like “$ source ~/Code/env/example_project && ~/Code/example_project/test_file.py” and what it outputs.

The last part is getting exec.py to run within Sublime Text 2 and making it to use vrun.sh to load the proper environments (shell and Python). For that I’ve placed my sublime_plugins repo into “~/Library/Application Support/Sublime Text 2/Packages/” so that Sublime recognizes the exec.py module. This module extends the init method of the AsyncProcess class of the supplied exec.py in order to make it possible to add the vrun.sh wrapper for our build scripts. The exec.py extensions tries to read the “exec_arg_list_wrapper” setting from the “Global.sublime-settings” file. Create one in your User folder and set it to the absolute path of the vrun.sh (read the file header of exec.py on how that works). Now you should be able to “build” a Python file via the build menu and see that it loads the proper environment before it finally executes it.

You can get the files from here:
bitbucket.org/theblacklion/sublime_plugins/src

Have fun,
Oktay.

0 Likes

Open Python files from selection or current line
#2

Hi.

do you and example of that?

I tried but I couldn’t manage to make it work.

thanks

0 Likes