1

I have a Python project in a subfolder within my git repo:

.git
api/
  requirements.txt
  .venv/
  myproj/
    __init__.py
    foo.py
    bar.py

The virtual environment is installed in the api folder which is a subfolder of my git repo.

In an interpreter, I can import from myproj/foo.py by doing:

from myproj.foo import baz

But in Vim, coc-python reports:

unresolved import myproj.foo

and the autocompletion list includes the parent directory, api!

api.myproj.foo
api.myproj.bar

When I do :CocCommand > python.startREPL I can run the import just fine, without prepending api.

I can't even think about how to debug this problem!

Any pointers?

2

CoC assumes that a directory containing .git/ is the root of your project.

According to the CoC docs:

Unlike VSCode vim doesn't have workspace support. The solution is resolve workspace folders from opened files.

This means that CoC uses a list of filenames which, if present in a particular directory, indicate that it might be the project's root.

If the variable is not present, CoC falls back to a config setting called coc.preferences.rootPatterns which, by default is:

[".git", ".hg", ".projections.json"]

To override this behaviour you can set the list to include the name of your virtual environment. In this case:

autocmd FileType python let b:coc_root_patterns = ['.git', '.venv']
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.