Visual Studio Code — Python Editors in Review | by Martin Thoma | Towards Data Science

Visual Studio Code — Python Editors in Review

I think I fell in love 😍

Visual Studio Code 1.50.1. The screenshot was taken by Martin Thoma.

Visual Studio Code is a free editor written by Microsoft. Its first release was in 2015, and boy did that one take off. It has over 28k questions on StackOverflow, is the 2nd most-used Python editor according to the 2019 Jetbrains survey, and the most popular one according to the 2019 StackOverflow survey. VS Code has its own marketplace, works on Windows, Linux, and Mac. Microsoft has created a powerful language server called Pylance about which you certainly also want to read something … but let’s first start with the basics.

When you first start VS Code, it offers you a couple of plugins that might be helpful. I directly installed the Python integration as well as the Sublime Text key bindings. Keep especially the key bindings part in mind when I write about shortcuts.

Speed

VS Code takes a bit more than a second to start. This means it’s way faster than PyCharm, but slower than Sublime Text. VS Code stores everything, even if you don’t hit Ctrl+S . This means you can at all times just close the editor. The next time you open it, it will tell you that there are unsaved changes 😍

The User Interface

In the screenshot above you can see what I have typically opened:

  • The editor where you can read the code. Of course with Syntax highlighting and line numbers.
  • The explorer where you can see the files of the workspace.
  • The activity bar is left of the explorer. Here you can switch the explorer against a search tool, a version control system (VCS) integration, the debugger, and others. This was the main reason why I didn’t like VS code in the beginning. However, you can simply hide it 😍
  • Right of the code, there is a minimap of the code. This can be helpful to jump to weird sections.

Command Palette

The command palette is the way to reach any functionality of the editor. No more searching in deeply nested menus ❤️

You can open the command palette with the key combination Ctrl + Shift + P . It looks like this:

Screenshot was taken by the author

Although I can’t quite pinpoint why, I like the command palette of Sublime Text slightly more and the one of PyCharm way less. To me, it feels as if the fuzzy search of Sublime Text is best. Visually, VS Code did a really good job.

Customizable Shortcuts!

VS Code has an official cheat sheet for the default key bindings (source) and I already mentioned that you can use the default key bindings of other editors. That is neat, but I want to be able to customize it completely to my needs.

And, of course, you can do that! VS Code even offers you a pretty nice interface which acknowledges that you might want different actions depending on the situation / the programming language.

The shortcut editor of VS Code. Screenshot was taken by the author.

Tab Interactions

You can close a tab with Ctrl+W , just like in Chrome. You can switch to the first tab with Alt+1 , to the second with Alt+2 , …

You can open a new file with Ctrl+N — just like you would open a new window in Chrome.

Jump to Line

Ctrl+G to jump to a line. This is super handy when you’re debugging.

Goto File

Ctrl+P to go to any file in the current workspace. With fuzzy search again 🎉

Find / Replace all

Ctrl+F to find something, Ctrl+H to replace. You can also use regex! Definitely a feature I don’t want to miss. This is pretty cool in combination with multiple cursors! ( Ctrl+Shift+L )

Zen mode

Shift+Shift+P and search for “Zen”. Within that mode, you have the code. No mini-map, no file explorer, no footer. This is helpful if you want to show another developer something. And you can adjust what is shown in Zen mode. For example, I still want to see the line numbers.

17 more shortcuts

Amy J. Andrews wrote this awesome article with 17 more shortcuts:

17 Useful Visual Studio Code Shortcuts to Boost Your Coding Speed

This is the cool feature you can expect from the #1 code editor in the world

medium.com

Autocompletion

VS Code + pylance offers an AMAZING autocompletion 💘 Please be aware that you need to tell it a little bit about what you’re doing. Use type annotations to help VS Code to help you. By the way, type annotations are awesome!

Autocompletion example of VS Code with pylance. Screenshot was taken by the author.

Signature hints

I sometimes want to get a hint about the signature of a function. In VS Code, you just hover over the name and you get it. The box contains the docstring 😍

The function signature hint when you hover over the name. Screenshot was taken by the author.

Jump to Definition

Click on whatever you’re interested in and press F11 . That’s it. It works. And works so well and smoothly! You can do this with anything: Functions, variables, 3rd party stuff, core library stuff.

Debugging

The debugging interface for Visual Studio Code is lightweight and has exactly what you need in most cases: (1) You can click in the gutter where you want to set your break points. (2) You can start the debugger by clicking in the activity bar on the play button. Here you can also see the current value of all variables. (3) Continue, step over, step into, step out, restart and stop the debugger. (4) Observe the output in the terminal.

Screenshot was taken by the author

WSL2 Integration

This plugin is so amazing, it deserves its own section:

Using WSL 2 with Visual Studio Code

September 3, 2019 by Matt Hernandez, @fiveisprime It’s been a couple of months since the initial betas for the Windows…

code.visualstudio.com

Diff Tool

Visual Studio Code has a pretty decent diff tool integrated. You can call it from the console with

code --diff file1 file2

Windows Users: You can also execute this from the Ubuntu console in WSL2!

Unit Test Integration

Hit Ctrl+Shift+P and Configure Tests :

Image by author

Then you will be able to run tests with a single click on a test within VS Code 😍

Customizations

Visual Studio code offers plenty of ways to customize. You have hundreds of settings, color themes, and plugins.

Themes

Ctrl+Shift+P , search for “theme”:

Dark and light themes, including solarized and Textmate 😍 Screenshot was taken by the author.

Settings

Screenshot was taken by the author

You can also customize things via a settings.json file. Here are some settings I like, using the Ubuntu Mono font:

{
"editor.renderWhitespace": "all",
"editor.fontFamily": "Ubuntu Mono",
"workbench.colorTheme": "textmate",
"python.formatting.provider": "black",
"python.languageServer": "Pylance",
"python.analysis.typeCheckingMode": "basic",
"python.defaultInterpreterPath": "/home/moose/.pyenv/shims/python",
"zenMode.hideLineNumbers": false,
"zenMode.hideStatusBar": false,
"python.analysis.extraPaths": [
"/home/moose/.pyenv/versions/3.8.6/lib/python3.8/site-packages"
],
"workbench.colorCustomizations": {
"terminal.background": "#373633",
"terminal.foreground": "#dfdbd2",
},
"terminal.integrated.fontFamily": "Ubuntu Mono derivative Powerline"
}

Workspaces

You can customize which files/folders are ignored within your workspace settings. For example, create a .vscode/settings.json and add the following content for Python projects:

{
"files.watcherExclude": {
"**/.pytest_cache/**": true
},
"files.exclude": {
"**/.mypy_cache": true,
"**/.pytest_cache": true,
"**/*.egg-info": true,
"**/mypy-report": true
}
}

Plugins

Finally, the time has come to talk about the marketplace and especially pylance!

pylance

Pylance is a Python language server. As a user, you don’t have to worry about it too much. Think of it as a plugin which gives you autocompletion, signature help, type checking and much more. It’s especially awesome if you use type annotations.

As a developer who is wondering how things work under the hood, I must appreciate this nice design from Microsoft. They factored out one hard part so that a separate team can work on it. In theory, this could be integrated into other editors/IDEs/services. In practice, the license forbids it. Also, defining such a language server interface allows developers to create language servers for other languages as well 🌟 Having this interface is a sign of good software architecture.

I had to enable the “basic” type checking mode for pylance in the user settings (Ctrl+Shift+P). Screenshot was taken by the author.

Rainbow Brackets

Rainbow brackets colors nested brackets:

It’s easier to read with colored brackets, isn’t it?

Path Intellisense

Path Intellisense autocompletes your paths while your in the editor:

Screenshot was taken by the author

GitLens

GitLens shows you information of the git history:

Do you notice the gray text? That is the name of the editor (me), when the edit was done and the first line of the commit message. Screenshot was taken by the author

If you hover over the message you can jump to the commit which opens a graphical diff 😍

A graphical diff of the commit highlighted before. Screenshot was taken by the author.

AWS Toolkit

The AWS Toolkit plugin adds information about S3 Buckets, Lambdas, Cloudwatch logs, and more to your activity bar:

The screenshot was taken by the author

Honorable Mentions

  • SQLTools: Running DB queries from within VS Code
  • Live Share: Pair programming support in Corona times. I haven’t tried this plugin so far, though.

Comparison: VS Code vs Sublime Text vs PyCharm

Compared with Sublime Text, VS Code initially felt a bit more heavy-weight to me. However, this might simply be due to the fact that I’m using Sublime Text since roughly 2010 (I’ve bought the license in 2014). Having 10 years of experience in one tool means that you get used to that tool quite a bit. Except for the fact that Sublime is incredible fast, I don’t see any advantage of it over VS Code.

Compared to PyCharm Professional, VS Code feels much more lightweight. It’s faster and the interface is cleaner. I’m not absolute certain, but pylance might also be better than the autocompletion of PyCharm. The advantages of PyCharm are in the edge cases: The database tool window, SciView, maybe some debugging features, and for sure the code inspections.

Summary

Microsoft did an amazing job with VS Code: It’s fast, customizable, has the necessary features, a marketplace, reasonable defaults, a clean interface, support for Windows/Mac/Linux and is for free. Kudos! I love it! 💜

What’s next

In this “Python for Beginners” series, we already explained how to use Python with WSL2 on Windows, how to use Python on Windows with Anaconda. This article presented Visual Studio Code as a good IDE for beginners and professionals.

Next, we should write a hello world application. I’m open to ideas 😁