Search code examples
pythonvisual-studio-codepython-importsyntax-highlighting

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.


Solution

  • 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