Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable python import sorting in VSCode

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.

I tried putting

"editor.codeActionsOnSave": {     "source.organizeImports": false }, 

in my user settings but that doesn't fix it.

Thanks!

EDIT- I would like to keep formatting on save on except for the imports

like image 475
Lukas Schmit Avatar asked Jan 03 '19 02:01

Lukas Schmit


People also ask

How do I turn off auto import in VS Code?

To disable auto imports, set "javascript. suggest. autoImports" to false .

How do I fix imports in VS Code?

Set the correct Python path in VSCode In order to fix Unresolved Import in VSCode, you have to set python. pythonPath key in the settings to the correct value. You can quickly open the settings. json editor by accessing File > Preferences or press Ctrl + , key combination.


1 Answers

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 

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"]   

where E402 represents "module level import not at top of file"

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   

EDIT : the arguments for the formatter should be passed as separate list items in quotes like ["--ignore","E402"] rather than [--ignore=E402]

like image 164
Yedhin Avatar answered Sep 23 '22 00:09

Yedhin