Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sublime text3 and virtualenvs

I have different virtualenv's (made with virtualenwrapper) and I'd like to be able to specify which virtualenv to use with each project.

Since I'm using the SublimeREPL plugin for custom builds, how can I specify which python installation to build my project with?

For example, when I work on project A I want to run scripts with venvA's python, and when I work on project B I want to run things with venvB (using a different build script).

like image 605
Doc Avatar asked Jul 25 '14 19:07

Doc


People also ask

How do I create a new build system in Sublime Text?

Sublime Text is able to run build programs such as 'make', either when a key in pressed (F7 by default), or when a file is saved. The build system to use can be select from the Tools/Build System menu. If a project is open, the selected build system will be remembered for the project.

How do I edit a build in Sublime Text 3?

sublime-build , and it will now be accessible in the build system menu. Select it, hit Ctrl B to build, and then hit Ctrl Shift B to run the resulting program. Or you can use a Build and Run option and call it by hitting Ctrl B , then selecting that option.


1 Answers

Hopefully this is along the lines you are imagining. I attempted to simplify my solution and remove some things you likely do not need.

The advantages of this method are:

  • Single button press to launch a SublimeREPL with correct interpreter and run a file in it if desired.
  • After setting the interpreter, no changes or extra steps are necessary when switching between projects.
  • Can be easily extended to automatically pick up project specific environment variables, desired working directories, run tests, open a Django shell, etc.

Let me know if you have any questions, or if I totally missed the mark on what you're looking to do.

Set Project's Python Interpreter

  1. Open our project file for editing:

     Project -> Edit Project 
  2. Add a new key to the project's settings that points to the desired virtualenv:

     "settings": {      "python_interpreter": "/home/user/.virtualenvs/example/bin/python"  } 

A "python_interpreter" project settings key is also used by plugins like Anaconda.

Create plugin to grab this setting and launch a SublimeREPL

  1. Browse to Sublime Text's Packages directory:

    Preferences -> Browse Packages... 
  2. Create a new python file for our plugin, something like: project_venv_repls.py

  3. Copy the following python code into this new file:

    import sublime_plugin   class ProjectVenvReplCommand(sublime_plugin.TextCommand):     """     Starts a SublimeREPL, attempting to use project's specified     python interpreter.     """      def run(self, edit, open_file='$file'):         """Called on project_venv_repl command"""         cmd_list = [self.get_project_interpreter(), '-i', '-u']          if open_file:             cmd_list.append(open_file)          self.repl_open(cmd_list=cmd_list)      def get_project_interpreter(self):         """Return the project's specified python interpreter, if any"""         settings = self.view.settings()         return settings.get('python_interpreter', '/usr/bin/python')      def repl_open(self, cmd_list):         """Open a SublimeREPL using provided commands"""         self.view.window().run_command(             'repl_open', {                 'encoding': 'utf8',                 'type': 'subprocess',                 'cmd': cmd_list,                 'cwd': '$file_path',                 'syntax': 'Packages/Python/Python.tmLanguage'             }         ) 

Set Hotkeys

  1. Open user keybind file:

     Preferences -> Key Bindings - User 
  2. Add a few keybinds to make use of the plugin. Some examples:

    // Runs currently open file in repl     {         "keys": ["f5"],         "command": "project_venv_repl"     },     // Runs repl without any file     {         "keys": ["f6"],         "command": "project_venv_repl",         "args": {             "open_file": null         }     },     // Runs a specific file in repl, change main.py to desired file     {         "keys": ["f7"],         "command": "project_venv_repl",         "args": {             "open_file": "/home/user/example/main.py"         }     } 
like image 79
Soytoise Avatar answered Oct 12 '22 00:10

Soytoise