Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Formatting on save moves import statment in VS-Code

I am learning flask and building a app based on Corey Schafer's Youtube tutorials. I'm using VS-Code as my editor of choice.

In tutorial 5 he splits his app into a package structure and when I do it I run into a problem. In my settings I enabled the "editor.formatOnSave": true and the formatter I use is autopep8. Now when I move one of my import statements from the top to the bottom of my file and save, the formatter moves it back to the top. I'm guessing its because of 'E402 - Fix module level import not at top of file'?

I tried to ignore it with the "python.formatting.autopep8Args": [--ignore=E402] but that did nothing. Maybe here you can only turn of lynting errors and E402 is only a lynting error and its not relevant for the formatting?

I hope you can help me disable or fix it.

Cheers

like image 221
ueberBrot Avatar asked Dec 02 '18 09:12

ueberBrot


People also ask

Why is VSCode formatting on save?

You have configured prettier to format as per the default settings. If you don't see the code formatted automatically on file save then it might be because you have multiple formatters installed in VS Code.

How do I enable auto format in VSCode?

Click the “Settings” gear icon in the bottom-left corner. Search “Formatter” and click the “Editor: Default Formatter” option. From the drop-down menu, select whichever code formatter you want to use. Scroll down a bit and check the box next to the “Editor: Format On Save” option.

How do you organize imports in VS code?

In VSCode, go to File -> Preferences -> Settings and click on the icon in the top right hand corner to open up the settings in JSON. Et voilà! Your imports will now be organized every time you save a file.


1 Answers

Brett Cannon comment:

I don't know autopep8 well, but that example is mis-formatted as you're missing quotes around the argument: "python.formatting.autopep8Args": ["--ignore=E402"]

@BrettCannon terrific! That fixed it. Tried so long to fix it that i forgot those quotes and didnt see that they were missing.

like image 194
ueberBrot Avatar answered Oct 01 '22 19:10

ueberBrot