简体   繁体   English

带有黑色的Python VSCode formatOnSave在WSL中不起作用

[英]Python VSCode formatOnSave with black not working in WSL

I am using WSL2 to work on some Python project, but black is not auto-formatting in VSCode (Remote - WSL).我正在使用 WSL2 处理一些 Python 项目,但黑色在 VSCode(远程 - WSL)中不会自动格式化。

System info:系统信息:

  • Base system: Windows 11 Pro x64基础系统:Windows 11 Pro x64
  • WSL2 system: Ubuntu-22.04-LTS WSL2 系统:Ubuntu-22.04-LTS
  • settings.json path in WSL: /home/<user>/.vscode-server/data/Machine/settings.json WSL 中的settings.json路径:/home/<user>/. /home/<user>/.vscode-server/data/Machine/settings.json /data/Machine/settings.json

My settings.json looks like:我的settings.json看起来像:

{
  // Python linting and formatting customizations..
  "python.linting.pylintEnabled": false,
  "python.linting.flake8Enabled": true,
  "python.linting.enabled": true,
  "python.linting.flake8Args": ["--max-line-length=100", "--ignore=E501"],
  "python.formatting.provider": "black",
  "python.formatting.blackArgs": ["--line-length", "100"],
  "[python]": {
    "editor.codeActionsOnSave": {
      "source.organizeImports": true
    },
    "editor.formatOnSave": true
  }
}

This same settings works on my Windows 11 system.同样的设置适用于我的 Windows 11 系统。 In local Windows, whenever I create a virtual environment for a python project and save a python file, vscode automatically asks me to install flake8 and black in the venv.在本地 Windows 中,每当我为 python 项目创建虚拟环境并保存 python 文件时,vscode 会自动要求我在 venv 中安装flake8black But nothing of the sort happens in WSL.但在 WSL 中没有发生任何此类事情。

Even if I install black in WSL Python venv, it does not auto-format the Python code;即使我在 WSL Python venv 中安装black ,它也不会自动格式化 Python 代码; and there are no logs related to black visible in either Output > Python or Output > Log (Main) tab.并且在Output > PythonOutput > Log (Main)选项卡中没有可见的与black相关的日志。

But manual formatting works after I install black in venv:但是在 venv 中安装black后手动格式化有效:

(venv) asif@MSI:~/vscode/myproject/$ black app_pkg/models.py 
reformatted app_pkg/models.py

All done! ✨ 🍰 ✨
1 file reformatted.

Note: I even tried adding "python.formatting.blackPath": "./venv/bin/black" to settings.json file, but it didn't work.注意:我什至尝试将"python.formatting.blackPath": "./venv/bin/black"添加到settings.json文件,但没有成功。

Any help would be much appreciated.任何帮助将非常感激。 Thanks in advance!提前致谢!

Edit: As suggested, I have these two settings enabled in my system, Format on save applied universally:编辑:正如建议的那样,我在我的系统中启用了这两个设置,普遍应用保存时的格式: 保存设置上的格式 Python code formatter set as black : Python 代码格式化程序设置为black Python 默认格式化程序设置

Still I am unable to make the Python formatOnSave work in VSCode-WSL.我仍然无法使 Python formatOnSave在 VSCode-WSL 中工作。

But manual formatting works after I install black in venv但是在 venv 中安装 black 后手动格式化有效

It can be seen that black can work.Now let's set up automatic formatting for vscode:可见黑色可以工作。现在我们来为vscode设置自动格式化:

First open the settings of vscode, search for format on save , and then check it.先打开vscode的设置, format on save ,然后勾选。 Set this step, and the code will be automatically typeset every time the file is saved.设置这一步,每次保存文件都会自动排版代码。

在此处输入图像描述

Then search Python formatting provider and select black .然后搜索Python formatting provider并选择black

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 formatOnSave 时如何使 VSCode 尊重 pyproject.toml 配置中的黑色排除文件 - How to make VSCode honor black excluded files in pyproject.toml configuration when using formatOnSave WSL 中的 VScode + pipenv + pyenv:调试器不适用于 Python,仅适用于非系统 Python 版本 - VScode + pipenv + pyenv in WSL : debugger not working with Python only for non-system Python version WSL 的 VSCode:解释器的错误 python 版本 - VSCode for WSL: wrong python version of the interpreter 如何配置Vscode使用WSL调试Python? - how to configure Vscode to debug Python using WSL? VSCode 在保存时不自动格式化(python - 黑色) - VSCode not autoformatting on save (python - black) vscode用诗歌和wsl找不到python - vscode can't find python with poetry and wsl Vscode 黑色格式化程序在诗歌项目中不起作用 - Vscode black formatter is not working in poetry project Formatter black 在我的 VSCode 上不起作用......但为什么呢? - Formatter black is not working on my VSCode...but why? Formatter Black 不适用于我的 VScode Jupyter Notebooks? - Formatter Black not working for my VScode Jupyter Notebooks? 在 vscode 中找不到 python 调试适配器 - WSL:Ubuntu - python debug adapter not being found in vscode - WSL:Ubuntu
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM