DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’» is a community of 966,904 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
Adam Lombard
Adam Lombard

Posted on • Updated on

VSCode: Setting line lengths in the Black Python code formatter

The docs for the Black Python code formatter say that the formatter "is not configurable". This is largely true, but if you have Black set up to work in VSCode, you can configure the line length.

In VSCode, go 'Code -> Preferences -> Settings' and search for "python formatting black args".

Add two separate arguments, in this order: --line-length and n, where "n" is your desired number of allowed characters per line:

Interface for adding Black formatting arguments in VSCode

A few notes about line lengths in Python:


If you are working on a shared project with a team, consider bypassing the VSCode settings entirely, and setting line lengths via the project's pyproject.toml:

[tool.black]
line-length = 119
Enter fullscreen mode Exit fullscreen mode

Top comments (7)

Collapse
 
perfecto25 profile image
mrx

fyi, can also add this to your .vscode folder > settings.json

{
    "python.formatting.provider": "black",
    "python.formatting.blackArgs": ["--line-length", "120"],
    "python.linting.enabled": true
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
jordanahaines profile image
Jordan Haines • Edited on

Definitely going to reference this very time I start a new project -- right after I re-learn that setting max line length in .pylintrc is not a substitute for the setting Adam's proposed here. Thanks Adam!

Collapse
 
adamlombard profile image
Adam Lombard Author

You're welcome, Jordan! πŸ™‚

Collapse
 
waylonwalker profile image
Waylon Walker

119 seems uncomfortable! Then again I have never done Django and might be missing some context here.

I generally stick to black defaults and change my flake8 config to match.

Collapse
 
adamlombard profile image
Adam Lombard Author

Yeah, 119 is a little wide. Apparently it's the max char width for GitHub code review, which is why the Django project uses it as a max line length.

Collapse
 
simkimsia profile image
simkimsia

Thank you this helps!

Collapse
 
adamlombard profile image
Adam Lombard Author

You're welcome!

Update Your DEV Experience Level:

Settings

Go to your customization settings to nudge your home feed to show content more relevant to your developer experience level. πŸ›