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:
Also I noticed that the Sort Imports option disappeared from the options I have when I right click:
I have not changed anything in VScode, any idea what might be wrong? Current VSCode Python extension version 2023.18.0
2
Answers
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 hereTo 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 sortkeyShift
+Alt
+O
.If someone wants to automate it completely you can add automatic import sorting on saving. Go to
Preferences
(Ctrl
+Shift
+P
) search forOpen User Settings (JSON)
. In the json file add this or modify the"[python]"
settings section if already exists: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.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:
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.