Skip to content

Sort Imports not working #22147

Closed
Closed

Description

@cbrnr

Type: Bug

Behaviour

Expected vs. Actual

The "Sort Imports" command in the context menu and the related command in the palette "Python Refactor: Sort Imports" should sort Python imports. However, these commands have no effect if the ms-python.isort extension is not installed as well.

The output panel contains the following message: Sort Imports: Please install and enable ms-python.isort extension to use this feature.

From a usability perspective, this behavior is not ideal. IMO, there are three solutions:

  1. Install the isort extension automatically with the Python extension.
  2. Disable the command until isort is installed
  3. Display an error message when running the Sort Imports command when isort is not available.

Steps to reproduce:

  1. From a clean Visual Studio Code environment, install the Python extension.
  2. Create a Python script with the following content:
    import math
    import abc
    
    print("Hello")
  3. Right-click anywhere in the script and select "Sort Imports" – nothing happens.
  4. Run "Python Refactor: Sort Imports" from the command palette – nothing happens.

Diagnostic data

  • Python version (& distribution if applicable, e.g. Anaconda): 3.11.5
  • Type of virtual environment used (e.g. conda, venv, virtualenv, etc.): Global
  • Value of the python.languageServer setting: Default
Output for Python in the Output panel (View→Output, change the drop-down the upper-right of the Output panel to Python)

2023-10-04 09:12:23.030 [info] Telemetry level is off
2023-10-04 09:12:23.030 [info] Experiments are disabled, only manually opted experiments are active.
2023-10-04 09:12:23.030 [info] Test server listening.
2023-10-04 09:12:23.030 [info] Python interpreter path: /usr/local/bin/python3
2023-10-04 09:12:23.135 [warning] Retry failed, storage update failed for key PYTHON_WORKSPACE_STORAGE_KEYS
2023-10-04 09:12:23.135 [warning] Retry failed, storage update failed for key PYTHON_WORKSPACE_STORAGE_KEYS
2023-10-04 09:12:23.483 [info] > conda info --json
2023-10-04 09:12:23.487 [info] > ~/.conda/bin/conda info --json
2023-10-04 09:12:23.491 [info] > ~/Library/r-miniconda-arm64/bin/conda info --json
2023-10-04 09:12:24.246 [info] Starting Pylance language server.
2023-10-04 09:12:38.151 [info] Telemetry level is off
2023-10-04 09:12:38.151 [info] Experiments are disabled, only manually opted experiments are active.
2023-10-04 09:12:38.198 [info] Test server listening.
2023-10-04 09:12:38.200 [info] Python interpreter path: /usr/local/bin/python3
2023-10-04 09:12:38.624 [info] > conda info --json
2023-10-04 09:12:38.641 [info] > ~/.conda/bin/conda info --json
2023-10-04 09:12:38.653 [info] > ~/Library/r-miniconda-arm64/bin/conda info --json
2023-10-04 09:12:39.390 [info] Starting Pylance language server.
2023-10-04 09:12:49.564 [info] Sort Imports: Please install and enable `ms-python.isort` extension to use this feature.
2023-10-04 09:13:37.135 [info] Sort Imports: Please install and enable `ms-python.isort` extension to use this feature.
2023-10-04 09:15:04.092 [info] Telemetry level is off
2023-10-04 09:15:04.093 [info] Experiments are disabled, only manually opted experiments are active.
2023-10-04 09:15:04.117 [info] Test server listening.
2023-10-04 09:15:04.168 [info] Python interpreter path: /usr/local/bin/python3
2023-10-04 09:15:04.562 [info] > conda info --json
2023-10-04 09:15:04.578 [info] > ~/.conda/bin/conda info --json
2023-10-04 09:15:04.583 [info] > ~/Library/r-miniconda-arm64/bin/conda info --json
2023-10-04 09:15:05.326 [info] Starting Pylance language server.
2023-10-04 09:15:40.936 [info] Sort Imports: Please install and enable `ms-python.isort` extension to use this feature.
2023-10-04 09:21:09.476 [info] Sort Imports: Please install and enable `ms-python.isort` extension to use this feature.
2023-10-04 09:21:29.030 [info] Telemetry level is off
2023-10-04 09:21:29.030 [info] Experiments are disabled, only manually opted experiments are active.
2023-10-04 09:21:29.096 [info] Test server listening.
2023-10-04 09:21:29.098 [info] Python interpreter path: /usr/local/bin/python3
2023-10-04 09:21:29.466 [info] > conda info --json
2023-10-04 09:21:29.484 [info] > ~/.conda/bin/conda info --json
2023-10-04 09:21:29.488 [info] > ~/Library/r-miniconda-arm64/bin/conda info --json
2023-10-04 09:21:30.233 [info] Starting Pylance language server.
2023-10-04 09:22:50.005 [info] Telemetry level is off
2023-10-04 09:22:50.005 [info] Experiments are disabled, only manually opted experiments are active.
2023-10-04 09:22:50.015 [info] Test server listening.
2023-10-04 09:22:50.016 [info] Python interpreter path: /usr/local/bin/python3
2023-10-04 09:22:51.194 [info] Starting Pylance language server.
2023-10-04 09:23:03.370 [info] Sort Imports: Please install and enable `ms-python.isort` extension to use this feature.
2023-10-04 09:23:06.376 [info] Sort Imports: Please install and enable `ms-python.isort` extension to use this feature.
2023-10-04 09:23:13.845 [info] Sort Imports: Please install and enable `ms-python.isort` extension to use this feature.

User Settings


languageServer: "Pylance"

testing
• pytestEnabled: true

terminal
• activateEnvironment: false

Extension version: 2023.16.0
VS Code version: Code 1.82.3 (fdb98833154679dbaa7af67a5a29fe19e55c2b73, 2023-10-02T11:09:17.749Z)
OS version: Darwin arm64 23.0.0
Modes:

System Info
Item Value
CPUs Apple M2 Pro (12 x 24)
GPU Status 2d_canvas: enabled
canvas_oop_rasterization: enabled_on
direct_rendering_display_compositor: disabled_off_ok
gpu_compositing: enabled
multiple_raster_threads: enabled_on
opengl: enabled_on
rasterization: enabled
raw_draw: disabled_off_ok
video_decode: enabled
video_encode: enabled
vulkan: disabled_off
webgl: enabled
webgl2: enabled
webgpu: enabled
Load (avg) 1, 1, 1
Memory (System) 32.00GB (0.65GB free)
Process Argv
Screen Reader no
VM 0%

Activity

  1. karthiknadig commented on Oct 4, 2023

    @karthiknadig
    Member

    Install the isort extension automatically with the Python extension.

    We used to do this. Most people don't use isort, and we got feedback to remove this as an extension dependency.

    Disable the command until isort is installed
    Display an error message when running the Sort Imports command when isort is not available.

    We will be removing this command entirely as there are more import sorting solutions now available as extensions. See, ruff for example.

  2. extrange commented on Oct 6, 2023

    @extrange

    Out of curiousity what other import sorting solutions are available? black has declared they will not be sorting imports, which leaves isort as the de facto import sorting tool.

    If that is so, then including isort as an extension dependency might reduce the number of additional extensions to manually install/declare in a devcontainer.json.

  3. cbrnr commented on Oct 6, 2023

    @cbrnr
    Author

    ruff can sort imports.

  4. karthiknadig commented on Oct 6, 2023

    @karthiknadig
    Member
  5. cbrnr commented on Oct 6, 2023

    @cbrnr
    Author

    Karthik Nadig (@karthiknadig) when is the removal of the command scheduled? It's a bit confusing, especially for people just starting with Python and Visual Studio Code.

  6. karthiknadig commented on Oct 6, 2023

    @karthiknadig
    Member

    This is scheduled for the october milestone.

  7. 01000101 commented on Oct 17, 2023

    @01000101

    Could someone please provide the steps to re-enable the isort functionality? Even with isort installed, the previous menu item is missing.

  8. karthiknadig commented on Oct 17, 2023

    @karthiknadig
    Member

    Joshua Cornutt (@01000101) That command will be removed. Use this instead:
    image

  9. karthiknadig commented on Oct 23, 2023

    @karthiknadig
    Member

    Verification Steps:

    1. Have latest pre-release version of python extension
    2. Open a python file.
    3. The editor context menu should not have Sort Imports command.
  10. locked as resolved and limited conversation to collaborators on Nov 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bugIssue identified by VS Code Team member as probable bugtriage-neededNeeds assignment to the proper sub-teamverifiedVerification succeeded

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions