Visual Studio Code is one of the most versatile code editors out there. Even though it's a code editor, the sheer extensibility of the program makes it almost as capable as some of the JetBrains products out there.
In this article, I'll walk you through the entire process of configuring Visual Studio Code for Python development. It's not a universal setup, but this is something that I use personally and have found it to be really comfortable.
The first step is to install Visual Studio Code on your computer. I'm on Debian 12 at the moment and I have the editor ready to go. Platform specific installation instructions are available in the documentation.
Assuming you are past the installation step, now I'll introduce you to a set of essential extensions that will elevate your Python development experience to the next level.
The first extension that you need to install is the Python Extension from Microsoft.
This is actually an extension pack that contains two extensions. The first extension is the Python extension. It lays the foundation for Python development in Visual Studio Code.
The other one is Pylance, which is a very performant language server for Python.
This extension provides rich intellisense support and is powered by Pyright, the static type checker from Microsoft. The next thing you need to think about is linting.
A linter is a program that analyses your code statically and provides valuable insights on possible errors.
The Pylance extension does an excellent job of finding out fatal errors within your code, but there is more to code than just that.
When working on a big project, it's pretty common to leave unwanted mess within your codebase. Things like unused imports and variables, bad code practices, and so on.
A good linter can point out code smells like this and make your code cleaner. Now, the go-to choice when it comes to Python linters is Pylint.
Pylint has been around for ages and works quite well, but I think there is a better alternative.
Ruff is an extremely fast Python linter written in Rust that imposes stricter linting rules than Pylint. The tool also has an official extension.
It's a plug n' play extension and doesn't require any additional configuration whatsoever. So once you have it installed, you're good to go.
Like a linter, isort is another utility that's sole purpose is sorting import statements.
The utility sorts all the imports alphabetically, while also dividing them into sections.
The extension is very straightforward. Once you have the extension, it'll render squiggly lines under any import statement that seems out of place.
You can then use the quick action menu to sort them. Or, you can also use the command palette to quickly access the isort command.
Mypy Type Checker
Before I start talking about this extension, let me explain what mypy actually is.
According to the info on their homepage:
Mypy is an optional static type checker for Python that aims to combine the benefits of dynamic (or "duck") typing and static typing. Mypy combines the expressive power and convenience of Python with a powerful type system and compile-time type checking.
In simpler words, mypy forces you to add essential type annotations to your Python programs, making them easier to comprehend.
Recently, Microsoft has published an extension that adds type checking functionality using mypy to their beloved editor.
Once you have installed the extension, it'll perform necessary checks on your code and report any missing type annotations as compile-time errors.
While having type annotations is not mandatory, it's highly recommended.
IntelliCode provides AI assisted code completion in Visual Studio Code. It may sound similar to GitHub Copilot, but in reality it's a lot smaller than that.
Where GitHub Copilot or Tabnine provides full-blown code blocks, IntelliCode autocompletes lines of code pretty flawlessly.
In most cases, this extension can help you type less of the same code by suggesting the right thing while also keeping out of your way.
While not related to Python specifically, Error Lens is a great extension that embeds errors right by the side of the line of code.
I often work on my 14 inch Thinkpad and like to turn off the terminal pane. Error Lens eradicates the need to look at the terminal now and then to see my errors and warnings.
As useful as it may be, sometimes your editor can look cluttered due to all the warning and error outputs, so decide accordingly.
Unlike other programming languages, an incorrect level of indentation can literally break your program in Python.
Visual Studio Code already does a good job of visualizing indentation levels within your code, but if you want to add some color to it, the indent-rainbow package is what you need.
It adds different colors to the different levels of indentation. Personally, I don't use this one on a regular basis, but you may find it useful.
Like I said, these extensions and my personal configuration are not a silver bullet. But this setup is something that I've been using for quite a while and I hope it's useful to you as well.
Or I install the Jupyter extension while working on a Jupyter Notebook. So feel free to install whatever you need, just don't overdo it.