VS Code’s “Sort Imports” fails with exception

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.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top