- Must-Have Packages and Settings in Sublime Text for a Python Developer
- In this post, I will show you awesome Sublime packages/plugins and some settings for your workflow which will make your life as a Python programmer much more easier.
- Python and Cython language bundles
- State of the Art
- Future plans
- Installation
- Contribute
- Appreciation
- License
Must-Have Packages and Settings in Sublime Text for a Python Developer
In this post, I will show you awesome Sublime packages/plugins and some settings for your workflow which will make your life as a Python programmer much more easier.
If you are one of those Awesome Python programmers who love Sublime Text then you have come to the right place. In this post, I will show you the settings and plugins I use as a Python programmer for Sublime. So let’s make your Sublime more awesome.
User Settings:
Open the Sublime Text personal settings file:
A file named Preferences.sublime-settings—User should open on the right-hand side. Add these lines to your User specific settings
These settings are self-explanatory but you aren’t getting the «rulers» part, so according to pep8, docstrings and comments should be limited to 72 characters and all other lines should be limited to 79 characters. So these lines will help you when you are crossing your limits, further read PEP-8.
Packages/Plugins
We will be setting up the following packages one by one:
- Sublime Linter
- SublimeLinter-flake8
- Djaneiro
- PyYapf Python Formatter
- Anaconda
1. First Install SublimeLinter and Flake8 plugin
These packages are used to lint your code. Wondering what is code linting ?
A code linter is a program that analyses your source code for potential errors. The kinds of errors a linter can detect include:
- syntax errors;
- structural problems like the use of undefined variables.
- best practice or code style guideline violations.
Install Flake8
Then just to make sure flake8 is correctly installed — run flake8 badly_formatted_python_program.py and it will show you all the error in your program
Install SublimeLinter
Open package control by pressing ‘ctrl’ + ‘shift’ +’p’ and type ‘install package’ then click on the first result that appears. Then search for SublimeLinter
and press ‘Enter’ to install. After installing ‘SublimeLinter, type this SublimeLinter-flake8 and install it. Restart your Sublime and after the previous steps completed, we can see the linting errors and warnings right in Sublime Text and update live as we code.
2. Install Djaneiro
Djaneiro supports Django templating and keyword highlighting and provides useful code snippets (tab completions) for Sublime Text. The snippet system is an incredible timesaver. You can create common Django blocks with only a few keystrokes for templates, models, forms, and views. Check out the official documentation to see a list of snippets. Docs-Djaneiro
Go to package control > install package and then search for package name Djaneiro
3. PyYapf Python formatter
I bet you are going to love this package as much as I do. First, let me introduce you to what is PyYapf, it is best explained in the README of it’s Github repository. Here is demo if you want to see it in action
Most of the current formatters for Python — e.g., autopep8, and pep8ify — are made to remove lint errors from code. This has some obvious limitations. For instance, code that conforms to the PEP 8 guidelines may not be reformatted. But it doesn’t mean that the code looks good. YAPF takes a different approach. It’s based off of ‘clang-format’, developed by Daniel Jasper. In essence, the algorithm takes the code and reformats it to the best formatting that conforms to the style guide, even if the original code didn’t violate the style guide. The idea is also similar to the ‘gofmt’ tool for the Go programming language: end all holy wars about formatting — if the whole codebase of a project is simply piped through YAPF whenever modifications are made, the style remains consistent throughout the project and there’s no point arguing about style in every code review. The ultimate goal is that the code YAPF produces is as good as the code that a programmer would write if they were following the style guide. It takes away some of the drudgery of maintaining your code
Sounds interesting? Let’s get started with the installation
Fire-up your terminal and install
Then open Sublime Text and as always open package control > Install package and search for package named PyYapf Python Formatter and install it.
Restart Sublime and select any poorly formatted python code and press ‘ctrl’+’alt’+’f’ and the code you selected should be reformatted according to the Python style guidelines. If you don’t select any line in and hit ‘ctrl’+’alt’+’f’ then your whole file will be reformatted (Cool Right ?)
Anaconda is ultimate python package because it adds a number of IDE like features to your Sublime Text Editor. To name a few, It includes
- Show Documentation
- Autocompletion
- McCabe code complexity checker
- Code Linting
- Goto Definition
Search for a package named Anaconda and install it. I use a different linter package (SublimeLinter-flake8, which we installed in Step 1 of this post) so I disable linting altogether within the user-defined Anaconda settings file, Anaconda.sublime-settings, via the file menu: Sublime > Preferences > Package Settings > Anaconda > Settings — User:
If you have installed both PyYapf and Anaconda then the ‘ctrl’+’alt’+’f’ may not work because Anaconda uses this keybinding for the «finding all the usages of the selected word». So, You have to change this shortcut to work with PyYapf. One way to do this is Go to Preferences > Package Settings > Anaconda > Key Bindings — Default and find command anaconda_find_usages then change its key value from [«ctrl+alt+f»] to [«ctrl+alt+a»]
# NOTE: If you are on Linux and facing some difficulty while installing or some of the packages are not working as expected then try installing the module with sudo’s H flag for e.g sudo -H install or you can comment down below and I would love to help.
That’s it from my side, Is there any package you want it to be added to this post? What are your favourite packages for Sublime?
Python and Cython language bundles
Bleeding-egde Python and Cython syntax highlighters and other goodies for Sublime Text 2/3 and TextMate editors.
(Or for any other editor, that is using the .tmLanguage syntax definition)
State of the Art
Below are some of the most important improvements compared to the existing syntax highlighters:
- Added better number highlighting:
- All types of floating point notations are working now;
- All types of complex number notations are working now;
- New types of binary and octal-number notations are supported;
- Long integer is removed.
- Better regex support (multiline, grouping, comments and more are improved).
- Format specifier mini-language
- Template strings
- Byte notation added;
- Proper string prefixes added.
Future plans
Installation
Via Package Control
The fastest and easiest way to install these packages for Sublime Text is the following:
- Install Package Control
- Open Tools → Command Palette
- Select Package Control: Install Package
- Search for Python 3 and Cython+ packages and install them
- Happy coding 😉
Set as default
After you installed the language definition file successfully, all you have to do is assign the .py files to always open with this syntax highlighter. Go to
View → Syntax → Open all with current extension as. → Python 3
To remove this setting, you can always overwrite this preference.
Manual installation
Download the tmLanguage files from the python and Cython branches of this repository. Navigate to your Packages folder and create a Python3 and/or a Cython folder(s) and copy the tmLanguage and sublime-build files into.
If you want to use my Work-In-Progress theme file: navigate to Packages/User folder and copy Gloom.tmTheme into it. Then go to user-settings, and change your old color theme to the new one.
Contribute
Any help is appreciated and more than welcome – my goal is to make this the ‘de facto’ language bundle for python 3. If you want to submit a change, please use the following conventions when editing the original python files:
- variables uses underscore_separated_names ;
- all files uses 4 spaces for indentation;
- = and : operators are aligned if length of variable names are similar;
- ( , [ and < start a new line, if possible and reasonable;
- each line must fit in the width 80 columns (code, text, etc.);
- comment separators can be easily generated with the src.utils.separator() function
Appreciation
Thank you very much @joncle (Jon Clements) for all the support in general, @kms70847 (Kevin), @Ffisegydd (Keiron Pizzey), @schesis (Zero Piraeus) and @poke (Patrick Westerhoff) for the feedbacks, and of course thanks for all the wonderful members of the sopython chat room for supporting me in any ways! May the Cabbage be with us 😉
I also want to thank for all the loyal users of this package, and especially @rahul-ramadas (Rahul Ramadas) and @bordaigorl (Emanuele D’Osualdo) who were very active on giving me feedbacks, reports, etc.
License
Copyright © 2013 — 2017 Peter Varo
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.