问题描述
我试图在保存文件时禁止 vscode 格式化我的 python 导入.我有一些代码必须在各种导入之间运行,所以顺序很重要,但每次我保存它时都会将导入推到顶部.
I am trying to disable vscode from formatting my python imports when I save my file. I have some code that must run in between various imports so order is important, but every time I save it just shoves the imports to the top.
我试着放了
"editor.codeActionsOnSave": {
"source.organizeImports": false
},
在我的用户设置中,但这并不能解决问题.
in my user settings but that doesn't fix it.
谢谢!
编辑-我想在保存时保持格式化,除了导入
EDIT- I would like to keep formatting on save on except for the imports
推荐答案
检查 vscode 设置中的以下设置,如果是 true 则将其设置为 false 以完全禁用保存时格式化,如下所示:
Check for the below setting in vscode settings, if it's true then set it to false for completely disabling formatting on save, like so :
"editor.formatOnSave": false
为了格式化和忽略不在顶部的导入,首先将上述设置设为 true 并添加到您的用户设置中,然后尝试将此设置添加到您的用户设置中,如果您使用 python 的默认格式化程序,那就是 autopep8 :
for formatting and to ignore imports not being at top itself, first make the above setting true and add to your user settings and try adding this setting to your user settings, if you're using the default formatter for python, that is autopep8 :
"python.formatting.autopep8Args": ["--ignore","E402"]
其中E402表示模块级导入不在文件顶部"
请注意,这仅在您使用默认格式化程序/linter 时才有效.如果您正在使用其他一些 linter,那么我建议您查看他们的文档以了解它是如何完成的.最常见的是可以使用全局配置文件,比如 $HOME/.config/.pycodestyle,并在那里添加必要的设置,例如:
Note that this would only work if you are using the default formatter/linter. If you are using some other linter then i suggest you look up their documentation to see how it's done. Like most commonly one could make use of global config file, say $HOME/.config/.pycodestyle, and add necessary settings there, like :
[pycodestyle]
ignore = E402
格式化程序的参数应作为单独的列表项在引号中传递,例如 ["--ignore","E402"] 而不是 [--ignore=E402]
这篇关于在 VSCode 中禁用 python 导入排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!