在VSCode中禁用python导入排序 [英] Disable python import sorting in VSCode

查看:483
本文介绍了在VSCode中禁用python导入排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我保存文件时,我试图禁止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表示模块级别导入不在文件顶部"

请注意,这仅在使用默认格式化程序/衬纸时有效.如果您使用的是其他短绒棉,那么我建议您查阅他们的文档,看看它是如何完成的.像最常见的情况一样,可以使用全局配置文件(例如$ 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导入排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆