skip to Main Content

My VSCode was working fine, I have a pyenv environment with a specific python version and installed dependencies which I was using and all was good and suddenly it stopped recognizing all imports. They are all whited out:

enter image description here

Also I noticed that the Sort Imports option disappeared from the options I have when I right click:
enter image description here

I have not changed anything in VScode, any idea what might be wrong? Current VSCode Python extension version 2023.18.0

2

Answers


  1. Chosen as BEST ANSWER

    Ok these seem to be fresh issues coming from VSCode and Python extension for VSCode.

    After doing a lot of digging around I thing both of the issues come from very very rescent changes from VSCode. I mean they were just released thsi month (October 2023).

    So regarding the Sort Imports option VSCode just stopped supporting it this month see reference and ticket opened on VSCode github here

    To be able to still get automatic sorting of our imports we need to explicitly install the isort extension in VSCode and Use the Organize Imports command with sortkey Shift + Alt + O.

    If someone wants to automate it completely you can add automatic import sorting on saving. Go to Preferences (Ctrl + Shift + P) search for Open User Settings (JSON). In the json file add this or modify the "[python]" settings section if already exists:

     "[python]": {
        ...Other settings...
        "editor.formatOnSave": true,
        "editor.codeActionsOnSave": {
          "source.organizeImports": true
        },
      },
    

    Now if you restart VSCode you should get automated sorting of improts on save. Also see some visual instructions here.

    Regarding the issue of imports not being recognized I downgraded the VSCode Python extension to a previous version 2023.2.0 and it worked.


  2. As for the import sorting not being in the command palette or context menu anymore, see the notes for the v2023.18.0 release (September 2023), which state:

    • Remove sort imports from command palette and context menu by @ludizhan in #22058

    If you read the Pull Request, it is for Drop the "Sort Imports" menu option #20233, where the rationale is that the context menu item and command palette command are redundant when there is a Code Actions menu item.

    The maintainers also got feedback that many users of the Python extension don’t use isort, so they’re removing isort support in the October 2023 release of the Python extension: Remove isort support #22187 (see also Sort Imports not working
    #22147
    ). See also https://github.com/microsoft/vscode-python/wiki/Migration-to-Python-Tools-Extensions.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search