asottile / reorder-python-imports

Rewrites source to reorder python imports
MIT License
737 stars 55 forks source link

Documentation on how to use with Visual Studio Code #117

Closed kbd closed 1 year ago

kbd commented 4 years ago

I'm trying to use reorder_python_imports with Visual Studio Code. Unfortunately, built-in support in the Python extension was already rejected due to lack of demand, but I'd still like to use it.

I've tried setting "python.sortImports.path": "reorder-python-imports", but that results in the error:

> reorder-python-imports ~/path/to/file.py --diff
Error: $PYTHONPATH set, import order may be unexpected

Fortunately (since it can't be changed by specifying python.sortImports.args), it looks like y'all already support --diff as an alias for --diff-only, but I don't see any arguments I can give to reorder_python_imports to make it work within the Python extension, which evidently sets $PYTHONPATH.

asottile commented 4 years ago

it's not a hard error: https://github.com/asottile/reorder_python_imports/blob/ea508a5085c91de194b4c2237a4dbcea3538625f/reorder_python_imports.py#L745-L747

are you seeing other issues?

shame that vs code rejected it, perhaps if someone stepped up with a patch and offered to maintain that component it would be reconsidered -- ale for instance felt it useful enough to add support CC @brettcannon @luabud

kbd commented 4 years ago

are you seeing other issues?

It's not reformatting in the editor despite it being a soft-error. Maybe it's because reorder_python_imports is printing the string to stderr so vscode isn't parsing the output properly. I assume vscode combines stdout+stderr when it reads the output of the program, because I made a script at ~/bin/reorder_python_imports that's just:

reorder-python-imports --diff-only "$@" 2>/dev/null

and then (after pointing "python.sortImports.path" at my script) it works in vscode. It also works if I stick a PYTHONPATH='' (but no stderr redirection) at the beginning of the command to remove the error.

asottile commented 4 years ago

the diff is produced on stdout

if vscode is blindly combining stdout and stderr that's their bug and not actionable to fix here

kbd commented 4 years ago

Here's the line where the vscode Python extension executes the formatter:

diffPatch = (await processService.exec(isort, args, { throwOnStdErr: true, token })).stdout;

So it just takes stdout but it looks like it throws on any stderr output. (Not going to continue digging through the code to confirm that's what throwOnStdErr actually does.)

Edit: not saying this is "your problem" or that you need to do something about it, just offering this for informational purposes.

brettcannon commented 4 years ago

"python.sortImports.path" is very much structured around isort, so I'm not surprised plugging in reorder_python_imports doesn't work out-of-the-box.

And we are always willing to re-evaluate the popularity of a project to consider adding support for it, but at our scale and team size supporting any tool is a maintenance burden so we have to be a bit strict on expanding what we support.

I will also say there's nothing magical here about what our extension provides. If someone wanted to write their own reorder_python_imports extension then that should work fine along side our extension.

TheButlah commented 4 years ago

After looking into this, it appeared as though the best approach was to create a vscode extension to add support for reorder-python-imports, so that's what I did: https://github.com/TheButlah/vscode-reorder-python-imports

Note that it still doesn't play nicely with python.sortImports. Everything works fine if you just run things from the command palette, context menu, or lightbulb feature, but if you turn on the organize imports on save, vscode runs both isort and reorder-python-imports. If you know what to do about this, please file an issue or post a comment!

TheButlah commented 4 years ago

Update: I've added support and documentation for using reorder-python-imports with the microsoft python extension's isort. My extension should now be feature complete and ready for use.

@asottile Any chance you'd be willing to add a section in the README for links to IDE/editor plugins, where you could put a link to my extension?

asottile commented 4 years ago

sure! how about a section right below this one

TheButlah commented 4 years ago

Looks good to me!