简体   繁体   中英

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

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...

The line argument for black is wrong, it should be

--line-length=120

according to https://stackoverflow.com/a/58048911/4435175 .

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

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...

但这是一个疯狂的猜测:我使用 sudo 从命令行手动启动了 dockerd:这给了我各种奇怪的问题(从容器内部创建的文件,例如迁移或缓存文件最终由 root 拥有。我最近将 dockerd 移动到服务中,现在突然黑色在保存时运行。万恶的根源可能是权利问题,这是因为 dockerd 作为 sudo 而不是作为服务运行的事实。

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

I use miniconda.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM