[Solved] VS Code Python extension v2023.18.0 stopped resolving all python imports and Sort Imports option not available – Python

by
Liam Thompson
python visual-studio-code

Quick Fix: Firstly, Install the isort extension in VSCode and Use the Organize Imports command (Shift + Alt + O).
Secondly, To automate the process on save, in VSCode settings add this code: "[python]": { "editor.formatOnSave": true, "editor.codeActionsOnSave": { "source.organizeImports": true }}
Finally, Restart VSCode to auto-sort imports on save.

The Solutions:

Solution 1: Downgrading Python Extension and Installing Isort

The sudden unrecognized imports and the disappearance of the Sort Imports option are linked to recent changes in VSCode and its Python extension.

To resolve these issues, consider the following steps:

1. Downgrading Python Extension:

  • Downgrade your VSCode Python extension to a previous version, such as 2023.2.0. This version has been confirmed to work without these issues.

2. Installing Isort Extension:

  • Install the isort extension in VSCode.
  • Use the Organize Imports command with the keyboard shortcut Shift + Alt + O to sort your imports automatically.

3. Automating Import Sorting:

  • To automate import sorting on save, add the following settings to your VSCode user settings (JSON file):
"[python]": {
    ...Other settings...
    "editor.formatOnSave": true,
    "editor.codeActionsOnSave": {
      "source.organizeImports": true
    },
  },
  • Restart VSCode to enable automated sorting of imports on save.

Following these steps should restore the recognition of imports and provide you with the Sort Imports functionality again.

Solution 2: Remove Redundant Features

The removal of the "Sort Imports" option from the command palette and context menu in VS Code Python extension v2023.18.0 is intentional, as stated in the release notes. The rationale behind this change is to eliminate redundancy, as there is already a Code Actions menu item that serves the same purpose.

Additionally, the Python extension will no longer support the isort package in its October 2023 release. This is due to the fact that many users of the Python extension do not use isort, and the maintainers are aiming to streamline the feature set.

As an alternative to the "Sort Imports" option, you can use the conventional "Organize Imports" command in the command palette, which is bound by default to Shift+Alt/Opt+O. This command will rearrange your imports in a consistent manner according to Python’s import sorting rules.

Solution 3: Edit JSON Setting to Organize Imports

The Sort Imports option has been removed from the command palette and context menu. To organize your imports, you can make changes to your JSON settings file. Here are three code snippets for different scenarios:

  1. Using Old SortImports:

    "[python]": {
    "editor.codeActionsOnSave": {
    "source.sortImports": true
    },
    }

  2. Using OrganizeImports (Removes Unused Imports and Organizes Them):

    "[python]": {
    "editor.codeActionsOnSave": {
    "source.organizeImports": true,
    "source.organizeImports.reorder-python-imports": true
    },
    }

  3. Using OrganizeImports (Not Using Isort but Sorting Imports):

    "[python]": {
    "editor.codeActionsOnSave": {
    "source.organizeImports": false,
    "source.organizeImports.reorder-python-imports": true
    },
    }

You can choose the code snippet that best suits your requirements and paste it into your VS Code settings.json file. After saving the changes, your imports will be organized when you save your Python code.

Q&A

What are possible reasons for the issues with import resolving and missing Sort Imports option in VS Code?

Recent changes in VSCode and the Python extension caused these issues. The Sort Imports option was removed and import resolving might be affected by a bug.

How to get automatic import sorting on saving?

To achieve automatic import sorting on saving, enable "editor.formatOnSave" and "editor.codeActionsOnSave" settings in VS Code.

How to sort imports in VS Code?

Use the conventional "Organize Imports" command in the command palette to sort imports, bound by default to "shift" + "alt" + "o" keys.

Video Explanation:

The following video, titled "How to Fix Python Was Not Found Run Without Arguments to Install ...", provides additional insights and in-depth exploration related to the topics discussed in this post.

Play video

I'm taking a Python Programming course, and I continued to experience this same error within Visual Studio Code with no solutions to resolve ...