SublimeText as a Python IDE.
I love Sublime Text - it is super responsive, customizable and elegant. But out of the box it is exactly what’s advertised - a text editor.
I also enjoy my recent journey into programming in Python. So can Sublime be my IDE for Python? Yes, it can - and in this article I’ll describe how I set it up.
IDE - what’s that?
To simplify it a bit - that’s just saying that the editor contains some features that make it more suitable for programming in a given language. So for example you get some hints, code completions, error correction, color coding, auto formatting etc. when you work. Super useful - no matter you’re starting our or you’re a pro with years of exerience. It just makes you code faster and with less lint.
Rocky start
First of all - what not to do…. I thought it will be a breeze - someone had to have done it before me, right? So I’ve jumped into depths of internet looking for a guide - and found quite a couple, usually recommending similar things, which was encouraging.
So I’ve installed recommended plugins… and it was not ideal. While it “kinda” worked, it was not perfect - for example often I got parts that were using newer Python functions marked as incorrect. And that was only the most obvious type of problems.
It was frustrating to the point I’ve started looking for alternatives outside Sublime. And got to admit - they were a bit more straightforward to set up. But none felt as good as ST. They did the trick but didn’t make me want to use them.
Simply put - initially I’ve did it wrong. I should have realized obvious thing - in programming things can change quite fast. Something that was good and up to date yesterday can be obsolete now.
Instead of checking tutorials and the most downloaded plugins I should have checked the dates and info whether given plugin is actively maintained and developed.
Thus I started researching and asking - especially Sublime Text forums turned out super useful.
Linting, code completion, docstrings
At the time of writing most of the stuff I need is being handled by language server (and no worries, it’s local :) ). Thus I had to install plugin that can handle those. The easiest way to install one in ST is to use PackageControl. Launch Sublime and just press “ctrl + shift + p” to invoke commands palette, then start typing “package” and choose:
Package Control: Install Package
Find LSP, choose it and hit enter.
Now comes the actual server - in this case for Python. Again use:
Package Control: Install Package
and find LSP-pyright
. It will download Pyright which currently is pretty popular choice.
I’d also recommend installing other LSP plugins at this time - for example for yaml, json, xml, css… All depends on the work you do and type of data you work with. And it clearly shows advantages of this approach - you can add extra languages / doctypes easily and it will be easier to add it to your editor as those language servers are often not tied to particular app. For example if someone comes up with better option than Pyright (and I know some will argue there already are) all that has to be done is to replace Pyright package.
Once done you’ll immediately see things happening - while typing in .py file you’ll get suggestions, the errors will be marked (even in the gutter - the vertical bar with line numbers) and you’ll get description of them. You’ll also be able to find places that use a chosen function / variable. Neat!
In case you don’t see those changes check whether you have node / npm installed as pyright needs those. You should have it working in no time.
Auto formatting
There’s one thing missing though - auto formatting. It corrects the code to make it nicer, more readable and in case of Python, PEP8 compliant. While some lsps offer this functionality Pyright does not.
Luckily there are other options. I’ve chosen python-black (Package Control: Install Package -> python-black). I totally love it. It can be a bit uncompromising at times, but it is fast and is able to solve situations that other solutions (autopep8 for example) struggle with. It breaks long lines crazily well. Sweet!
Build with Terminus
Ok, so the last bit. The coding experience is already nice and the code could be run straight away. There’s one issue though - interactive, command line apps that require user input won’t be able to get it. The panel in which Sublime Text runs the code does not allow entering input.
So you’d have to launch an external terminal and start the app there… Luckily there’s a better option. We can have a full blown terminal inside Sublime and we can even set it up so the code will be run (“built”) there.
Actually there’s an awesome video on the subject here:
https://www.youtube.com/watch?v=etIJMVIvVgg
Done!
That’s it - ST turned into nice Python dev environment. While the process is IMO slightly more involved than in some of the other choices I think it’s worth it.
Of course you can push it way farther - add more extensions, set up hotkeys, get new themes and so on. Definitely recommend it :) - it’s blazing fast, works great and is super customizable.