Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vscode black formatter is not working in poetry project

I have these settings in vscode for the black extension in a poetry project, which uses system cache and poetry venv.

    "editor.formatOnSave": true,
    "python.formatting.provider": "black",
    "python.formatting.blackPath": "path-to-/bin/black",
    "python.pythonPath": "path-to-/python",
    "python.linting.mypyEnabled": true,
    "python.linting.mypyPath": "path-to-/bin/mypy"

I cannot understand why the formatter formats nothing. I am using local workspace settings ( above ).

like image 600
godhar Avatar asked Feb 01 '26 19:02

godhar


2 Answers

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

like image 170
Molly Wang-MSFT Avatar answered Feb 03 '26 08:02

Molly Wang-MSFT


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/"
like image 36
godhar Avatar answered Feb 03 '26 08:02

godhar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!