问题
I\'m totally new with sublime3, but i couldn\'t find anything helpful for my problem...
I\'ve differents virtualenvs (made with virtualenwrapper) and I\'d like to be able to specify which venv to use with each project
Since I\'m using SublimeREPL plugin to have 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)
sorry my terrible english...
回答1:
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
Open our project file for editing:
Project -> Edit ProjectAdd 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
Browse to Sublime Text's
Packagesdirectory:Preferences -> Browse Packages...Create a new python file for our plugin, something like:
project_venv_repls.pyCopy 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
Open user keybind file:
Preferences -> Key Bindings - UserAdd 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" } }
回答2:
There is a sublime text3 package, named Virtualenv, allowing you to build using the Python from your virtualenv.
It supports any versions of Python in your virtualenv, and works very well for me (MacOS).
To install it, we just command+Shift+P to call out pacakge control (install it if you don't have it yet), then type install. Next type virtualenv, when you see it appears click return to install it.
After installing it, select Tools --> Build System --> Python + Virtualenv. Then you can use command + B to execute your Python projects.
Click Here to check further information.
回答3:
You're looking for custom build systems.
From the menu bar, click Tools -> Build Systems -> New Build System...
Fill out the template it gives and save it under any filename ending in .sublime-build to your User folder.
Here is the documentation for making custom build systems:
http://docs.sublimetext.info/en/latest/reference/build_systems.html
I recommend making a custom build system for python scripts, then add variants for each virtual env you want. (see variants http://docs.sublimetext.info/en/latest/reference/build_systems.html#variants)
One you make a build system, you can switch them from
Tools -> Build Systems
(if not auto detected) and use the Command Palette (default ctrl + shift p) to switch between variants.
The only "gotcha" is the "cmd" parameter to describe what command to run. By default it takes an array of strings to run as a command, but you can use "shell_cmd" instead to just use a string of how you would run it via command line.
回答4:
I have an alternative. Just creating a new 'Build System' which runs as if in the virtual environment. Here we call it 'my_python'. The aim is to use this build system to run my script directly without leaving sublime. You need to:
- First,
preferences->Browse Packages. This will open a folder under which lies setting files. Go inside dirUserand create a new file namedmy_python.sublime-build(composed of the build system name followed by.sublime_build. After doing this, go toTools -> Build Systemand you will see a new optionmy_python. Add the following JSON settings to that file.
{ "shell_cmd": "/Users/Ted/bsd/vector/.v_venv/bin/python -u \"$file\"", "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)", "selector": "source.python" }Inside
shell_cmd, change/Users/Ted/bsd/vector/.v_venv/bin/pythonto the path of your python virtual environment.Then just use the short key to build your script.
When you want to change your virtual environment, just copy its path to the setting file and all done. Maybe the approach seems containing a lots of work, but it works well and convenient.
来源:https://stackoverflow.com/questions/24963030/sublime-text3-and-virtualenvs