Setup Black and Isort in VSCode
Assuming you have installed VSCode and Python interpreter on your machine, here are the steps in setting up Black and Isort in VSCode:
1. Install Python Extension
In Mac, use command + shift + x to open Marketplace. Search for Python Extension, and install it.
2. Install Black, and Isort
Note: You can create and use a new virtual environment or install it globally. In this setup, my python environment is at ./workspace_venv/bin/python3
$ pip3 install black$ pip3 install isort
3. Edit settings and configure Black and Isort. command + shift + p
User settings editor will pop-up and you should configure it with something like this:
editor.formatOnSave: true formats document on save
- python.pythonPath points to the virtual environment where Black and Isort are installed
- python.formatting.blackArgs: [“ — line-length=99”] configures line-length for Black.
NOTE: I personally set line-length/line_length
to 99. Black’s line-length default value is 88. I-sort’s default line_length is 79.
- python.sortImports.args: [“ — profile”, “black”] Starting Isort 5.0, we we can easily configure Isort to match Black’s settings by passing args
"--profile", "black"
- source.organizeImports: true runs Isort automatically upon saving your document
- Edit (25th April 2021):
Update:
You can configure Black and Isort’s settings on pyproject.toml.
Inside your root’s project folder, create or update your pyproject.toml file to include
There! You can now enjoy a formatted code upon saving your document. ✨