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:
A few notes about line lengths in Python:
- PEP8 recommends a line length of 79 characters (72 for docstrings)
- Black sets line lengths to 88 characters by default
- The Django docs recommend a maximum line length of 119 characters (79 for docstrings)
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
Was this helpful? Did I save you some time?
Top comments (9)
I'm setting the arguments via
pyproject.toml
and had the problem, that the line-length was not reformatted properly.After a while, I found out that I have to restart the format server!
If you have the same problem...simply do this:
'Ctrl + Shift + P' -> '> Black Formatter: Restart Server'
Thank you!!!
fyi, can also add this to your .vscode folder > settings.json
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!You're welcome, Jordan! π
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.
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.
Thank you this helps!
You're welcome!