question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Failing to sort imports with `isort`

See original GitHub issue
[coc.nvim] coc-pyright 1.1.164 with Pyright 1.1.165`
Workspace: /project
Using python from /project/venv/bin/python

[Info  - 2:26:57 PM] Pyright language server 1.1.165 starting
[Info  - 2:26:57 PM] Server root directory: /Users/user/.config/coc/extensions/node_modules/coc-pyright/node_modules/pyright/dist/
[Info  - 2:26:57 PM] No configuration file found.
[Info  - 2:26:57 PM] No pyproject.toml file found.
[Info  - 2:26:57 PM] Setting pythonPath for service "main": "/project/venv/bin/python"
[Warn  - 2:26:57 PM] stubPath /project/typings is not a valid directory.
[Info  - 2:26:57 PM] Assuming Python version 3.9
[Info  - 2:26:57 PM] Assuming Python platform Darwin
[Info  - 2:26:57 PM] Searching for source files
[Info  - 2:26:57 PM] Auto-excluding /project/venv
[Info  - 2:26:57 PM] Found 3 source files
########## isort Output ##########
Error from isort:
Traceback (most recent call last):
  File "/Users/user/.config/coc/extensions/node_modules/coc-pyright/pythonFiles/sortImports.py", line 11, in <module>
    import isort.main
ModuleNotFoundError: No module named 'isort'

########## isort Output ##########
Error from isort:
Traceback (most recent call last):
  File "/Users/user/.config/coc/extensions/node_modules/coc-pyright/pythonFiles/sortImports.py", line 11, in <module>
    import isort.main
ModuleNotFoundError: No module named 'isort'

########## isort Output ##########
Error from isort:
Traceback (most recent call last):
  File "/Users/user/.config/coc/extensions/node_modules/coc-pyright/pythonFiles/sortImports.py", line 11, in <module>
    import isort.main
ModuleNotFoundError: No module named 'isort'

Getting an error: [coc.nvim] Failed to format import by isort when running :CocCommand python.sortImports

The error is not expected because isort exists and works within the project’s virtual environment:

$ which isort
/project/venv/bin/isort

$ isort .
Fixing /project/main.py

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:11 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
yaegassycommented, Sep 7, 2021

@hexcowboy Please set the value of python.sortImports.args as array, not as “string”.

{
  // ...snip
  "python.sortImports.args": ["--profile", "black"]
  // ...snip
}
0reactions
hexcowboycommented, Sep 7, 2021

Reopening because this issue is still persisting and I was able to reproduce it in a VM.

Reproduce:

  1. Fresh install neovim and vim-plug
  2. Create the ~/.config/init.vim with the following contents
call plug#begin(stdpath('data') . '/plugged')
Plug 'neoclide/coc.nvim', {'branch': 'release'}
call plug#end()
  1. Run :PlugInstall
  2. Install coc-pyright with :CocInstall coc-pyright
  3. Write the following config to ~/.config/nvim/coc-settings.json
{
  "pyright.organizeimports.provider": "isort",
  "python.sortImports.args": "--profile black"
}
  1. Create a virtual environment and activate it
$ python3 -m venv venv
$ source venv/bin/activate
(venv) $ pip install isort
(venv) $ python --version && isort --version
Python 3.9.7
isort 5.9.3
  1. Open NeoVim with a Python file (sample fiile)
from typing import Literal
import os
  1. Run :CocCommand python.sortImports Screen Shot 2021-09-07 at 2 52 32 PM
Read more comments on GitHub >

github_iconTop Results From Across the Web

Sort Import fails with an exception traceback #14546 - GitHub
When I try to sort imports on a python script, it fails with the following traceback: Traceback (most recent call last): File ...
Read more >
VS Code's "Sort Imports" fails with exception - Stack Overflow
Open a python script with multiple import lines in VS Code: import sys import os from pprint import pprint · Open command palette...
Read more >
Exceptions - isort
__init__( f"isort was told to sort imports within code that contains syntax errors: " f"{file_path}." ) self.file_path = file_path class IntroducedSyntaxErrors( ...
Read more >
Use isort to sort your Python module imports automatically
isort is a Python library to sort imports alphabetically, and automatically separated into sections and by type. Before we use isort to format...
Read more >
Sorting imports with isort — Perfect Python - YouTube
Welcome to Perfect Python, the series where I show you how to take your code to the next level — perfection.In this episode...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found