Setting up python dev environment in Sublime Text 3
Recently I had to completely reset my Sublime Text 3 project settings and set everything up from scratch. I find the following two features the most important and need to be configured first:
- linting -- letting the computer keep track of consistent coding style
- definition lookup -- letting the computer to look up location of definitions
Linting is provided by SublimeLinter [1] and function definition lookup by Anaconda Python IDE [2]. This article assumes Package Control [3] is already been installed.
Prerequisites
Install flake8 code analyzer [4] on the system for both Python 2 and 3, it's required by SublimeLinter.
apt-get install python-flake8 python3-flake8
Sublime Package Installation
Install the following Sublime packages: SublimeLinter, SublimeLinter-flake8 and Anaconda.
For each package repeat the following process -- Invoke package control by bringing command pallet with Ctrl+Shift+p start typing install, then select Package Control: Install Package type package name and hit enter.
Project Preferences
These are per-project settings, go to Project -> Edit Project and paste the following config. If Edit Project item is unavailable, save the project with Project -> Save Project As...
Top-level settings sections
SublimeLinter -- all the settings related to SublimeLinter
build_systems -- Default Anaconda build system, configuration parameters to run external program to process project files [5]
folders -- Sublime declaration on folders included in the project.
settings -- Anaconda interpreter / project settings
Other parameters
The configuration parameter @python specifies syntax version to lint, in this case Python 2.7.
In all paths, replace {user} and {project} with actual user and project names. I follow a convention of using project name as the name of the virtual environment for that project.
extra_paths -- parameter specifies one or more path to a virtual environment in order to make declaration lookup work for third-party libraries. [6]
{
"SublimeLinter":
{
"@python": 2.7,
"linters":
{
"flake8":
{
"max-line-length": 120
}
}
},
"build_systems":
[
{
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"name": "Anaconda Python Builder",
"selector": "source.python",
"shell_cmd": "\"/home/{user}/.virtualenvs/{project}/bin/python\" -u \"$file\""
}
],
"folders":
[
{
"path": "/home/{user}/repos/{project}"
}
],
"settings":
{
"python_interpreter": "/home/{user}/.virtualenvs/{project}/bin/python",
"extra_paths": ["/home/{user}/.virtualenvs/{project}/"],
}
}
Key Shortcut for Anaconda 'Goto definition..' lookup
Anaconda Goto definition.. action is different from from Sublime Text 3 Built-in Go To Definition.., the latter works just fine for project source files, but it doesn't allow for settings in external paths.
The former can be triggered either by right-clicking on a term, then selecting Anaconda -> Goto Definition, or using default key binding Ctrl + Alt + g; I find both of the shortcuts inconvenient to use.
The configuration below assigns this action to F8 key (which doesn't seem to be used by anything).
Paste the following code in Preferences -> Package Settings -> Anaconda -> Key Bindings - User
[
{
"command": "anaconda_goto",
"keys": ["f8"],
"context": [{"key": "selector", "operator": "equal", "operand": "source.python"}]
}
]
Now set cursor on a term with definition to lookup and press F8.
[1] | SublimeLinter 3 -- http://www.sublimelinter.com/en/latest/ |
[2] | Anaconda Python IDE -- http://damnwidget.github.io/anaconda/ |
[3] | Package Control, the Sublime Text package manager -- https://packagecontrol.io/ |
[4] | Flake 8 the modular source checker, pep8, pyflakes and co -- https://pypi.python.org/pypi/flake8 |
[5] | Sublime Text Unofficial Documentation / Build Systems (Batch Processing) -- http://docs.sublimetext.info/en/latest/file_processing/build_systems.html |
[6] | Configure Anaconda the Right Way -- http://damnwidget.github.io/anaconda/anaconda_settings/ |