Visual Studio Code for Python and Data Science? Top 3 Plugins You Must Have

Visual Studio Code for Python and Data Science? Top 3 Plugins You Must Have

Originally posted on towardsdatascience.

Is it the best code editor for Python and Data Science?

Are you struggling to find an optimal code editor for Python programming and data science? You’re not alone. There’s a ton of options to choose from — both free and paid — and today I’ll show you my favorite free one.

It’s Visual Studio Code — a completely free code editor from Microsoft. It’s by far the most flexible and feature-rich code editor I found. It even has more features than PyCharm Community, which is supposed to be a professional IDE. And don’t even get me started on Jupyter — it’s probably the best way to write notebooks, but notebooks alone aren’t enough for data professionals.

Today you’ll see my go-to approach for setting up VSCode for Python programming and data science. We’ll start with the installation and go through the plugins from there.

Download and install Visual Studio Code

Head over to code.visualstudio.com to download VSCode. The webpage should detect your OS automatically, so you only need to hit the big blue download button:

Image 1 — Visual Studio Code home page (image by author)

On Mac, it will download a ZIP file which you can extract and drag the app to the Applications folder. On Windows, I assume you need to open the .EXE file and click on Next a couple of times, and for Linux, it’s probably either a Terminal command or a DEB file.

Here’s what you should see once you launch it:

Image 2 — Visual Studio Code (image by author)

You can create a Python file by opening any folder on your machine, right-clicking the left sidebar and selecting New file. I’ve created a folder on Desktop and a main.py file in it:

Image 3 — Creating a Python script in VSCode (image by author)

By default, you won’t have the best debugging options, nor you’ll have IntelliSense. You also won’t be able to select a virtual environment. Fixing this is quick and easy. You only need to install a plugin. But let’s address something more urgent first.

Download a theme (optional)

The first thing I like to do in VSCode is to change the theme. It has nothing to do with Python and data science, so you can either skip this section or consider it as a bonus point.

The default theme is just too Microsofty for me. To change it, you can click on the Extension tab and search for themes. I particularly like the One Dark Pro theme, which is free, even though it says Pro:

Image 4 — One Dark Pro theme (image by author)

Click on the theme and hit the Install button. The default skin is a bit too light for my liking. On Mac, you can press CMD+K CMD+T to open the theme dropdown. My favorite is One Dark Pro Darker:

Image 5 — Using a custom theme (image by author)

Much nicer, isn’t it? Let’s address the extensions next.

Official Python extension

This one is a must-have if you want to work with Python. Go to the Extensions tab once again and search for Python. You should install the official extension from Microsoft:

Image 6 — Official Python extension (image by author)

You’ll now have a much easier time writing Python files. You can also choose a virtual environment now, which is something you’ll do daily. Click on the bottom left text that says Python 3.9.7 64 bit (at least on my machine) and select any environment you want:

Image 7 — Choosing a virtual environment (image by author)

Do you know what the best part is? You can immediately start working with Jupyter Notebooks! Create a .ipynb file to verify — it might prompt you to install some additional dependencies, so just agree to everything.

Once installed, you can enter Python code to a cell to verify everything works:

Image 8 — Testing Jupyter Notebooks (image by author)

And that’s it — you now have the basics installed, so you can work with Python either through scripts or through notebooks. Let’s add some additional functionality next.

Python docstring generator

One essential tip for writing good Python code is documentation. VSCode can help you with that. All you need to do is to install the Python Docstring Generator extension.

Image 9 — Python Docstring Generator extension (image by author)

Let’s see how it works. You’ll write a dummy Python function that sums up two integers:

def add(a: int, b: int) -> int:
    return a + b

Write the function inside main.py:

Image 10 — Dummy Python function (image by author)

You can now add a docstring by writing three double quotes below the function declaration and selecting the Generate Docstring option:

Image 11 — Docstring generator (image by author)

It will immediately write the boilerplate for you:

Image 12 — Docstring boilerplate (image by author)

All you’ll have to now is to edit the description and the role each parameter has:

Image 13 — Editing the docstring (image by author)

That’s much easier than writing everything from scratch. Maybe you don’t see the benefit because we have only one function, but imagine you had multiple Python modules, each having dozens of functions — then this extension is a huge time saver.

Python linter

And finally, I want to discuss linting. You can enable linting in VSCode to automatically tell you if you’re not following Python conventions. It will tell you if you have unused imports, variables, or if there’s anything to improve in your code.

To start, open up the Command Palette (Settings — Command Palette… or press SHIFT + CMD + P) and type in Linter. Choose the Select Linter option:

Image 14 — Selecting Python linter (image by author)

PyLint is the most popular one, so just click on it:

Image 15 — Linter options (image by author)

It will ask you to install PyLint if it’s not installed already. You’ll have to repeat the process for every virtual environment, so keep that in mind:

Image 16 — Installing PyLint (image by author)

Let’s now delete the add() function and explore what PyLint has to offer. You’ll import json and random modules and print a random integer between 1 and 100:

import json 
import random

print(random.randint(1, 100))

Here’s how it should look like:

Image 17 — Warning messages (1) (image by author)

You’ll see warning messages as soon as you save the file. The print statement complains because there’s no new line after it, but that’s a quick fix — just hit Enter at the end of the line.

The top import statement is underlined because we don’t have a file-level docstring on the top, so let’s write one quickly:

Image 18 — Warning messages (2) (image by author)

The warning won’t go away if you save the file. It now complains that you’ve imported json but aren’t using it in the file:

Image 19 — Warning messages (3) (image by author)

The message will go away once you delete the unused import.

To summarize, linters can help you write better Python code and make sure you’re following all the conventions. Your code will still run if the linter gives you warning messages, but it’s annoying to look at them, so you’ll likely address them as they come.