[Django]-Vscode black formatter is not working in poetry project

3👍

Make sure black is installed in current used environment.

Open a integrated Terminal and activate the venv, run pip show black to see if it’s installed in current environment. If not,

1.Comment these two settings;

"python.formatting.provider": "black",

"python.formatting.blackPath":"path-to-/bin/black",

2.Turn to python file, right click choose Format Document With… –> Python, there would be a prompt popping up to mention you install formatter, choose install black. After installation, the following setting will occur automatically in the Settings.json:

"python.formatting.provider": "black"

Then you can Format Document.

enter image description here

1👍

I found out that you must set the default formatter which is language specific.
For python this is ms-python.python extension by Microsoft, which allows a specific formatter to be enabled, e.g autopep8, black, yapf, etc. Note I was getting notification telling me that Extension 'prettier - Code formatter' cannot format file.py

"[python]": {
    "editor.defaultFormatter": "ms-python.python",
}

Then include your actual formatter:

"python.formatting.provider": "black",
"python.formatting.blackPath": "/path/"
👤godhar

0👍

If you’re developing in docker you can add

RUN ln -s $(poetry env info -p)/bin/black /usr/local/bin/black

and in your settings.json use the link

...
"python.formatting.provider": "black",
"python.formatting.blackPath": "/usr/local/bin/black",

which will use your black settings in your pyproject.toml (i.e) something like

[tool.black]
line-length = 80
target-version = ['py39']

Leave a comment