Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sort Imports not working #22147

Closed
cbrnr opened this issue Oct 4, 2023 · 10 comments · Fixed by #22187
Closed

Sort Imports not working #22147

cbrnr opened this issue Oct 4, 2023 · 10 comments · Fixed by #22187
Assignees
Labels
bug Issue identified by VS Code Team member as probable bug triage-needed Needs assignment to the proper sub-team verified Verification succeeded
Milestone

Comments

@cbrnr
Copy link

cbrnr commented Oct 4, 2023

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 (ViewOutput, 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%
@github-actions github-actions bot added the triage-needed Needs assignment to the proper sub-team label Oct 4, 2023
@karthiknadig karthiknadig self-assigned this Oct 4, 2023
@karthiknadig
Copy link
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.

@github-actions github-actions bot added the info-needed Issue requires more information from poster label Oct 4, 2023
@extrange
Copy link

extrange commented Oct 6, 2023

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.

@cbrnr
Copy link
Author

cbrnr commented Oct 6, 2023

ruff can sort imports.

@github-actions github-actions bot removed the info-needed Issue requires more information from poster label Oct 6, 2023
@karthiknadig
Copy link
Member

Use this extension: https://marketplace.visualstudio.com/items?itemName=charliermarsh.ruff
/cc @charliermarsh

@github-actions github-actions bot added the info-needed Issue requires more information from poster label Oct 6, 2023
@cbrnr
Copy link
Author

cbrnr commented Oct 6, 2023

@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.

@github-actions github-actions bot removed the info-needed Issue requires more information from poster label Oct 6, 2023
@karthiknadig karthiknadig added this to the September 2023 Recovery 1 milestone Oct 6, 2023
@karthiknadig karthiknadig added the bug Issue identified by VS Code Team member as probable bug label Oct 6, 2023
@karthiknadig
Copy link
Member

This is scheduled for the october milestone.

karthiknadig added a commit that referenced this issue Oct 10, 2023
This feature is now moved to `ms-python.isort` extension.

For #22183
Closes #22147
@01000101
Copy link

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

@karthiknadig
Copy link
Member

@01000101 That command will be removed. Use this instead:
image

@starball5
Copy link

@karthiknadig karthiknadig added the verification-needed Verification of issue is requested label Oct 23, 2023
@karthiknadig
Copy link
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.

@joaomoreno joaomoreno added verified Verification succeeded and removed verification-needed Verification of issue is requested labels Oct 25, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 25, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue identified by VS Code Team member as probable bug triage-needed Needs assignment to the proper sub-team verified Verification succeeded
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants