-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Wrong order in import when using sort imports in vscode #14254
Comments
@lfrodrigues, thanks for letting us know about this. We'll look into this as soon as we can. In the meantime, please provide the content of your settings.json file. |
I believe the The following example uses no workspace settings. My user settings that apply to Python follow: {
"python.formatting.blackArgs": ["-l 90"],
"python.formatting.blackPath": "/home/flyte/.local/bin/black",
"python.formatting.provider": "black",
"python.linting.pylintArgs": ["--max-line-length=100"],
"python.linting.flake8Args": ["--max-line-length=100"],
"python.sortImports.args": [
"--line-length=90",
"--use-parentheses",
"--trailing-comma",
"--multi-line=3"
],
"python.pythonPath": "/usr/bin/python3",
"python.dataScience.askForKernelRestart": false
}
Directory Structure:
import os
import requests
import foo
pass Sorting imports from VSCode in
and subsequently sorts the imports as follows: import os
import foo
import requests
pass If we execute
then the imports are sorted correctly: import os
import requests
import foo
pass If we
then the imports are sorted wrong again: import os
import foo
import requests
pass |
Here's my settings.json
|
I encountered same problem. I found that the isort package itself behaves differently when I was using isort==4.3.8 vs sort==5.5.3. If I call isort==4.3.8 in command line, everything is as expected. I think VSCode bumped the isort version it internally uses, so you see this incorrect order of imports. In your settings.json, try to add this and see if it works for you:
|
@mariadb-JeffBachtel It worked for me. however this setting is no where documented and is a necessary for using isort in vscode(at first the behavior is different and you don't know why or In workspaces having code in src like my case setting to that folder worked). maybe documenting this for troubleshooting also helps(and less risky than the patch). |
Yeah, I'm curious what @ericsnowcurrently thinks is the best way forward, but I'm glad it worked for you as a workaround |
For me, the solution by @mariadb-JeffBachtel partially solves my problem, but not completely. I wondering if the root of the problem is that I installed my modules with
If I add
to my settings (per @mariadb-JeffBachtel 's suggestion ) and then try to sort imports, I get:
So now it has designed the module I'm in ( |
As @juliussimonelli suggests:
by adding the following to vscode setting.json resolved the issue, at least for me. |
the however, setting the path to isort in my venv did work :-/ "python.sortImports.path": "/Users/ME/.local/share/virtualenvs/PROJECT-kZt1N2hz/bin/isort" |
Same for me there. I think VSCode is not using the isort from the selected python interpreter. I also tried setting "python.sortImports.path": "${config:python.pythonPath} -m isort"
"python.sortImports.path": "${config:python.pythonPath}/../bin/isort"
But without better results ... What I ended up doing is "python.sortImports.path": "${workspaceFolder}/.venv/bin/isort" Assuming that all my projects use a venv in |
There are a bunch of issues with how we handle import sorting. The plan going forward is to move import sorting via isort behind LSP. This gives us better control over where isort runs (currently we use the directory where the file we are sorting exists as the cwd). LSP also makes it easier for us to apply text edits. Lastly, VS Code uses organize imports code action, which we are not using at the moment, switching to LSP will give us that. Marking this issue as needs proposal, so we can discus this with the team. |
Hi everyone please try out https://marketplace.visualstudio.com/items?itemName=ms-python.isort extension and see if it fixes the issue. |
For anyone running into this problem please try: https://marketplace.visualstudio.com/items?itemName=ms-python.isort . Tat extension will soon replace the experience you see in python extension. |
Sorting using the
Python Refactor: Sort Imports
doesn't generate a correct output.When I run the same command in the console the output is correct.
Environment data
python.languageServer
setting: PylanceExpected behaviour
When I sort in the console:
I get:
Actual behaviour
When I sort using the vscode command
Python Refactor: Sort Imports
the I get:Steps to reproduce:
Just try to sort imports as I explained above and it should happen.
Logs
Here are the logs from vscode
The text was updated successfully, but these errors were encountered: