問題描述
我試圖在保存文件時禁止 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 導入排序的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!