Visual Studio Code> Python > Black formatting does not run on save
Asked Answered
C

4

15

Although I seem to have configured both linitng and formatting properly, the linting runs but the black formatting does not.

    "python.linting.flake8Enabled": true,
    "python.linting.flake8Args": [
        "--max-line-length=110"
    ],
    "python.linting.lintOnSave": true,
    "python.formatting.provider": "black",
    "python.formatting.blackPath": "/usr/local/bin/black",
    "python.formatting.blackArgs": [
        "--line-length",
        "110"
    ],
    "editor.formatOnSave": true,

I have checked, that black is properly configured and I can run the formatting from the command-line without issues.

But when saved in the IDE, black does not format the file. It also does not complain, there are no error-messages that pop up and nothing showing up in the logs. It just does not run the formatting at all.

I am running this insisde a docker-container using remote-editing: not sure, if this makes a difference.

The black-version is:

black>=19.3b0

and the vscode-version is 1.36.1.

Thx for any help, as right now I am pretty clueless, why the black-formatting does not run...

Clemens answered 15/7, 2019 at 12:23 Comment(4)
What is black8? The Python supports black but not any tool named black8. And did you check the Output panel to see if there were any errors logged there?Dishonorable
black8 was just a typo, the configuration properly references black. And no, there is nothing showing in any of the output panels.Vitkun
@Vitkun what do you mean by "remote-editing"? Are you using the Remote-Container extension from VS Code?Dishonorable
yes, I'm running a django-app inside a container and use the remote-editing from vscode to run the editing inside this container.Vitkun
T
7

The line argument for black is wrong, it should be

--line-length=120

according to https://mcmap.net/q/279172/-vs-code-python-black-formatter-arguments-python-formatting-blackargs.

Tl answered 28/9, 2020 at 8:58 Comment(0)
C
7

Late answer, in case it helps anybody. I had the same problem and resolved it as below

I needed to have this line in user settings.json

"editor.defaultFormatter": "ms-python.python"

Otherwise, I guess, the formatting was overwritten by my default formatting option which was

"editor.defaultFormatter": "esbenp.prettier-vscode",

So my overall settings for formatting related stuff were as below

"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.formatOnSave": true,
  "[python]": {
    "editor.formatOnSave": true,
    "editor.defaultFormatter": "ms-python.python"
  },
  "[javascript]": {
    "editor.formatOnSave": true,
    "editor.defaultFormatter": "esbenp.prettier-vscode"
  },

  "python.formatting.provider": "black",
  "python.formatting.blackPath": "/home/<user>/anaconda3/bin/black",

This line "editor.defaultFormatter": "ms-python.python" could also have been activated as below from VS-Code command-pallette (Ctrl + shift + P).

Format Document with > Configure Default formatter > Select Python

Corybantic answered 14/1, 2021 at 8:49 Comment(0)
V
0

But here is a wild guess: I started the dockerd manually from the command-line using sudo: This gave me all kind of weird issues (files created from inside the container e.g. migrations or cache-files ended up being owned by root. I recently moved the dockerd into a service, now suddenly black is running on save. The root of all evil could have been rights-issues, that come from the fact that the dockerd was run as sudo and not as a service.

Vitkun answered 23/7, 2019 at 9:40 Comment(0)
A
0

I add the blackPath: "python.formatting.provider": "black", "python.formatting.blackPath": "C:/conda/Scripts/black.exe", and solve this issue.

I use miniconda.

Atombomb answered 5/1, 2023 at 4:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.