DEV Community

Isaac P
Isaac P

Posted on

Configure Python Formatter and Linter for a VSCode Dev Container

tl;dr

Code example here:
Gist of only devcontainer.json
Full Example Repo – I recommend cloning and opening with Github Codespaces to see this in action

Goal

I had a goal to set up a python coding environment, pre-configured with all needed tools to develop, lint and format my code. I found that with a devcontainer this could be a relatively frictionless process and be ready to go immediately following the initial devcontainer build process.

Benefit

The top benefits I want from this is twofold. First is a clear coding standard easily adopted that ensures consistent code styling and practices. Second is fast and easy onboarding. This ensures that as I load my code, I can get up to speed quickly and tools are configured automatically.

Drawbacks

This approach is really convenient, but it is attached to VsCode and docker devcontainers. So it's not super portable. Also linting and formatting are opinionated topics, so inevitably someone is going to disagree with my approach.

For me these drawbacks are no big deal, the benefit outweighs the drawbacks.

How to set up

The devcontainer.json holds all the configuration for this to work.

The fastest way to see this work is by cloning the full example repo linked above, then opening with Github Codespaces. It will build and everything will be ready to go. You can test and see that linting and formatting happen automatically on file save, as dictated by the rules set in the devcontainer.json file.

If adding a .devcontainer directory and file into an existing project in VsCode, bring up the Command Palette. On mac it’s ⇧⌘P Then search for Reopen in Container. This should do all the work for you.

Be sure you have this extension installed: ms-vscode-remote.remote-containers

The initial build process takes a few minutes, works on all platforms (M1 mac tested).

This example uses the Docker in Docker dev container. There are others that may suit you better, feel free to customize to your needs. This example doesn’t rely on the exact type of dev container used.

Top comments (0)