Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Imports are incorrectly sorted and/or formatted | VS Code | Python

In a few of my beginner projects this strange red line underscoring one or more of my imports keeps appearing almost randomly and I can't figure out why. As the module is working perfectly fine it shouldn't have something to do regarding which Folder I open VS Code in as it can get resolved, so sys.path should also have the right path, as far as I'm concerned. Sometimes it works when I switch my imports around but often it just underscores a single import or switching them around doesn't do anything. Also when I try to let VS Code sort them with isort, nothing happens and nothing had ever happened.

like image 394
fabestah Avatar asked Jun 19 '26 23:06

fabestah


2 Answers

Edit: I realized that recently the isort extension from Microsoft was automatically added to my extensions and this has caused the annoying error to start showing. It may be that the extension is conflicting somehow with the isort library installed in your venv. The extension isn't needed, so I've just disabled it and no longer encounter this error.

It seems like this error started happening after I recently updated my VS Code to 1.73.0 (Insiders). I was able to get around it by splitting up my imports so that they don't get auto-formatted to be on multiple lines.

Here's an example:

Before the "fix", notice the squiggly red line with the annoying error: enter image description here

After the "fix", no more squiggly red line:

enter image description here

like image 182
Wesley Cheek Avatar answered Jun 21 '26 13:06

Wesley Cheek


I have had the same issue.

seems to be related to what's been discussed here I got around warnings by telling the formatter to ignore the error-- i.e. put this in your vscode config :

"python.formatting.autopep8Args": ["--ignore=E402"],
like image 42
Tofusoul Avatar answered Jun 21 '26 12:06

Tofusoul



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!