![](/img/trans.png)
[英]How to make VSCode honor black excluded files in pyproject.toml configuration when using formatOnSave
[英]Python VSCode formatOnSave with black not working in WSL
我正在使用 WSL2 处理一些 Python 项目,但黑色在 VSCode(远程 - WSL)中不会自动格式化。
系统信息:
settings.json
路径:/home/<user>/. /home/<user>/.vscode-server/data/Machine/settings.json
/data/Machine/settings.json 我的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
}
}
同样的设置适用于我的 Windows 11 系统。 在本地 Windows 中,每当我为 python 项目创建虚拟环境并保存 python 文件时,vscode 会自动要求我在 venv 中安装flake8
和black
。 但在 WSL 中没有发生任何此类事情。
即使我在 WSL Python venv 中安装black
,它也不会自动格式化 Python 代码; 并且在Output > Python
或Output > Log (Main)
选项卡中没有可见的与black
相关的日志。
但是在 venv 中安装black
后手动格式化有效:
(venv) asif@MSI:~/vscode/myproject/$ black app_pkg/models.py
reformatted app_pkg/models.py
All done! ✨ 🍰 ✨
1 file reformatted.
注意:我什至尝试将"python.formatting.blackPath": "./venv/bin/black"
添加到settings.json
文件,但没有成功。
任何帮助将非常感激。 提前致谢!
编辑:正如建议的那样,我在我的系统中启用了这两个设置,普遍应用保存时的格式: Python 代码格式化程序设置为
black
:
我仍然无法使 Python formatOnSave
在 VSCode-WSL 中工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.