Setting up VSCode for Python Development

A stupid-simple, barebones setup using:

Written for macOS since that’s what most of our devs use. Paths and shortcuts will vary for Windows and Linux.

Using this guide will help VSCode warn you of problematic code via red squiggles. You can then get an explanation via the “Problems” panel (Cmd+Shift+m.)

In a Terminal

# Install poetry globally in your Python (which you should
# manage with pyenv)
pip install poetry

# Create a new project
poetry new my_project

# Install these as development dependencies. Which they are.
poetry add black flake8 isort pytest --dev

In VSCode

Tell VSCode where all the virtual environments are

On a Mac, type Cmd+Shift+p to pull up the Command Palette. Search for python.venvPath and set it to

~/Library/Caches/pypoetry/virtualenvs

Set the Python Interpreter to point at the Virtual Environment

Type “Python Interpreter” in the Command Palette, hit enter, and pick something that looks like my_project-33ebFI0f-py3.9. You can see exactly which one by typing this in the terminal:

poetry env info -p

For the next steps, and on a Mac, type Cmd+, to open up your Editor Settings.

Use black

Search for python.formatting.provider and choose black.

You might like to format automagically on save. To do this, search for editor.formatOnSave.

Use isort

Nothing to do! Cmd+Shift+p and type “sort imports”.

Use flake8

Search for python.linting.flake8Enabled and enable Flake8.

Use pytest

Search for python.testing.pytestEnabled and enable PyTest. You’ll see a new “Beaker” icon in the Activity sidebar. Click it to see all the tests. VSCode has pretty powerful testing setup and discovery options you might be interested in.

Use mypy

Search for python.linting.mypyEnabled and enable MyPy.


The setup above should give you a nice Borg-like experience with Python development but you can make things even better using proprietary closed-source software!

PyLance

This is a language server for Python and is the intelligence in IntelliSense.

To install, just get it from the VSCode Marketplace. It won’t work out of the box until you add this to .vscode/settings.json[1] in your project root.

{
    "python.analysis.useImportHeuristic": true
}

Now enjoy the awesome benefits of a language server. E.g. If you’re on macOS and a symbol doesn’t resolve, Cmd+. is your friend.

Note: That setting above is in beta so you may have to type “Restart: Python Language Server” once in a while in the Command Palette if you get yellow squigglies that don’t go away EVEN THOUGH YOU KNOW YOU RIGHT, BOO đŸ˜¤

ReWrap

Install this extension. Then make sure you have 79 in the array of rulers in editor.rulers in your VSCode Settings. Now (on a Mac) press Option+q to rewrap (ha) comments to fit into 79 columns.

And Finally!

Exclude some unnecessary things in your sidebar. Pull up your VSCode settings (Command+, on macOS) and add these patterns

**/__pycache__
**/.pytest_cache

[1]: DO NOT CHECK THAT FILE AND FOLDER INTO SOURCE CONTROL.