python - 在 vscode 中使用 Prettier 时格式化 python 的问题
问题描述
在 vscode 中,我想使用 Prettier 作为我的默认格式化程序,但不适用于 Python,我将只使用 autopep8。我现在有以下设置:
{
"workbench.iconTheme": "vscode-icons",
"workbench.editorAssociations": [
{
"viewType": "jupyter.notebook.ipynb",
"filenamePattern": "*.ipynb"
}
],
"git.confirmSync": false,
"editor.formatOnPaste": true,
"editor.formatOnSave": true,
"editor.defaultFormatter": "esbenp.prettier-vscode",
"python.formatting.provider": "autopep8",
"explorer.confirmDelete": false,
"python.showStartPage": false,
"explorer.confirmDragAndDrop": false
}
当我保存 python 文件时,它给了我消息:“扩展 'Pretier - 代码格式化程序无法格式化等......'。所以,显然它仍然对 python 文件使用错误的格式化程序。我该如何改变这个?!
解决方案
如果我禁用 Prettier 作为默认格式化程序,它将不再在保存时格式化,但我的 Python 将在保存时由 autopep8 格式化。考虑到这一点,以下解决方案对我有用,既让 Prettier 为其他语言工作,又让 autopep8 为 Python 工作:
{
"workbench.iconTheme": "vscode-icons",
"workbench.editorAssociations": [
{
"viewType": "jupyter.notebook.ipynb",
"filenamePattern": "*.ipynb"
}
],
"git.confirmSync": false,
"editor.formatOnPaste": true,
"editor.formatOnSave": true,
"python.formatting.provider": "autopep8",
"explorer.confirmDelete": false,
"python.showStartPage": false,
"explorer.confirmDragAndDrop": false,
"python.linting.pylintArgs": ["--load-plugins=pylint_django"],
"javascript.updateImportsOnFileMove.enabled": "always",
"editor.defaultFormatter": "esbenp.prettier-vscode",
"[python]": {
"editor.defaultFormatter": "ms-python.python"
}
}
如果有人找到更好的解决方案,请告诉我!
推荐阅读
- css - 重新计算宽度:换行后自动
- c# - 隐藏窗口C#winform时如何打印出CMD命令的输出?
- ffmpeg - 在 MSYS2 MinGW 上构建 ffmpeg-4.3.2 时出现许多链接错误
- python - 卡在列表理解上
- python - 检查一个数是否可以由一个数和它的倒数之和组成
- javascript - React.js 使用 onClick 显示数据
- django - 无法更新 Django Rest Framework 中的多对多字段
- apache-spark - Apache Spark 仅使用机器上 4 个 vCPU 中可用的 8 个内核中的一个内核
- javascript - Javascript:使用反引号(`)构建字符串查询,但总是添加一个新行
- java - 如果没有使用 lambda 的重复值,如何从映射中过滤键值对