[英]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:系统信息:
settings.json
path in WSL: /home/<user>/.vscode-server/data/Machine/settings.json
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 中安装
flake8
和black
。 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 > Python
或Output > 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
:
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.