1

Whenever I invoke the "Sort Import" from the Visual Studio Code command palette, I get an exception stack trace:

Traceback (most recent call last): File "/Users/absingh/.vscode/extensions/ms-python.python-2020.10.332292344/pythonFiles/pyvsc-run-isolated.py", line 30, in <module>

The stack trace of the exception is:

Traceback (most recent call last):
  File "/Users/absingh/.vscode/extensions/ms-python.python-2020.10.332292344/pythonFiles/pyvsc-run-isolated.py", line 30, in <module>

Error 2020-11-04 12:15:45:     runpy.run_path(module, run_name="__main__")
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/runpy.py", line 252, in run_path
    return _run_module_code(code, init_globals, run_name, path_name)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/runpy.py", line 82, in _run_module_code
    mod_name, mod_fname, mod_loader, pkg_name)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/runpy.py", line 72, in _run_code
    exec code in run_globals
  File "/Users/absingh/.vscode/extensions/ms-python.python-2020.10.332292344/pythonFiles/sortImports.py", line 12, in <module>
    import isort.main
  File "/Users/absingh/.vscode/extensions/ms-python.python-2020.10.332292344/pythonFiles/lib/python/isort/__init__.py", line 2, in <module>
    from . import settings
  File "/Users/absingh/.vscode/extensions/ms-python.python-2020.10.332292344/pythonFiles/lib/python/isort/settings.py", line 31
    FILE_SKIP_COMMENTS: Tuple[str, ...] = (
                      ^
SyntaxError: invalid syntax

I tried switching the Language Server, but it does not help.

Steps to reproduce:

  1. Open a python script with multiple import lines in VS Code:
    import sys
    import os
    from pprint import pprint
    
  2. Open command palette by pressing [Command] + [Shift] + [p]
  3. Search for "Sort Import"
  4. Select and execute the command
Gino Mempin
  • 25,369
  • 29
  • 96
  • 135

1 Answers1

0

It's because you are using Python 2.x

                                                            |
                                                            |
                                                           \!/
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/runpy.py", line 252, in run_path
    return _run_module_code(code, init_globals, run_name, path_name)

while the module used to do "Sort Imports" only supports Python 3.6+. Let's start with this error from the isort module, which does the actual sorting of imports for the Python extension:

  File "/Users/absingh/.vscode/extensions/ms-python.python-2020.10.332292344/pythonFiles/lib/python/isort/settings.py", line 31
    FILE_SKIP_COMMENTS: Tuple[str, ...] = (

That's a variable type hint, which was only introduced in Python 3.6 (see What are variable annotations in Python 3.6?)

You need to switch to a Python environment with at least Python 3.6. (As a side note, unless you really need to, you shouldn't be using Python 2.7 anymore because it is no longer supported as of Jan 2020). You mentioned the new Pylance language server, but it does not matter whether you use Pylance or Microsoft as the language server. The problem is your Python version.

It is mentioned in the VS Code docs that isort is used:

Sort Imports

Sort Imports uses the isort package to consolidate specific imports from the same module into a single import statement and to organize import statements in alphabetical order.

It is mentioned in the Python extension Changelog that they upgraded to isort 5.5.2 for the September 2020 release, along with dropped support for some older Python versions:

Enhancements

...
8. Upgraded to isort 5.5.3. (#14027)

Code Health

...
3. Drop support for Python 3.5 (it reaches end-of-life on September 13, 2020 and isort 5 does not support it). (#13459)
5. Disable sorting tests for Python 2.7 as isort5 is not compatible with Python 2.7. (#13542)

Finally, the isort 5.0.0 release notes mentions minimum Python version support:

isort now requires Python 3.6+ to run but continues to support formatting on ALL versions of python including Python 2 code.

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
  • Thanks @Gino. I had been using iSort with py2 earlier and didn't know that the support for py2 was deprecated. I can confirm that iSort works on a py3 interpreter. – Abhishek Singh Nov 05 '20 at 16:24