coc-python
coc-python copied to clipboard
Unresolved import
Hello. I got a problem when having the following CocConfig:
{
"suggest.enablePreview": true,
"python.jediEnabled": false,
"python.linting.flake8Enabled": true,
"python.linting.pylintEnabled": false,
"suggest.timeout": 5000
}
I got en error that I have an unresolved import
. I think this may be a problem with MLPS? But in VS code I don't get this error:
This also makes so I don't get suggestions for
gym_minigrid
. Enabling Jedi get rid of the error but the suggestions still won't work. Any suggestions?
I also have a lot of unresolved imports. It seems to have to do with the directory structure of my project. Here's an example:
project-root
├── .git
├── bin (python3 venv)
│ └── python
└── src
├── file1.py
└── file2.py
So I have my venv at the root folder of the project, but the code in a src folder. This is what happens:
# src/file1.py
import file2 # warning, unresolved import, no hover/suggestions
import src.file2 # no warning, errors when running
So the language server seems to run the current file as if it were in the project root.
With previous versions of coc.nvim, a workaround was to place a .git
file in src/
, but this doesn't work anymore.
Is this a bug or am I supposed to configure the plugin in some way to fix this?
EDIT: I'm starting vim from the project root. If I start vim from src/
it works in this case. But I guess it would still be a problem if I my source code was in multiple directories
Checkout https://github.com/neoclide/coc.nvim/wiki/Using-workspaceFolders, you can use b:coc_root_patterns
to make coc.nvim resolve current root for you.
I've tried using that but I couldn't make it work either. How would I set it up for the example above?
I have a .git in my project root btw, I forgot to include that in the directory tree.
If you can't make rootPattern to work for resolve workspaceFolder, you can
- Run
:CocList folders
. - Press
<cr>
and edit the path to correct folder. - Run
:CocRestart
to restart service. - Save a session file to persist workspaceFolders.
Alright, thanks. But isn't this a really common scenario that should work out of the box? Having files in different directories that you would like to edit from the same vim instance, started from the project root.
I don't get how workspaceFolders is the solution to this problem.
Sorry for hijacking your issue @Joellidin. Is your issue the same as mine?
I don't get how workspaceFolders is the solution to this problem.
workspaceFolders allows language server works with different projects in one editor session.
Ideally user should start vim in project root, but many people don't work like that, and that also won't work with gui vim when user start the gui instance by click editor icon.
workspaceFolders of coc.nvim checks root patterns in cwd at first, so if you want it work out of the box, you need specify root patterns for python and add the required file/folder in project root.
VSCode create workspaceFolder by use add the folder from gui which would be worse user experience compare to root patterns for vim.
So what should be the project root for a project like this for it to work?
project-root
├── .git
└── src
├── directory1
│ ├── file1.py (import file2)
│ └── file2.py (import file1)
└── directory2
├── file1.py (import file2)
└── file2.py (import file1)
If I set the workspaceFolder to src/directory1
the imports in that directory work but not the ones in src/directory2
. The same thing goes for the other way around.
I would have guessed that the solution should be to set the workspaceFolder to something common to both of them, like src/
or even project-root
but then none of the imports work.
I think the issue here is that the language server seems to run each file opened as if it were in the workspaceFolder, even if it's inside some directory inside of the workspaceFolder. When it tries to resolve imports it won't find the neighbouring files.
Checkout https://github.com/neoclide/coc.nvim/wiki/Using-workspaceFolders You can configure root patterns for a filetype, then create that file/folder inside each root folder of python files to make resolve works.
I think the issue here is that the language server seems to run each file opened as if it were in the workspaceFolder.
It should work like this, since it needs to know the root folder of each file for file resolve, I can't understand what's your problem.
When it tries to resolve imports it won't find the neighbouring files.
You should use relative import.
You can configure root patterns for a filetype, then create that file/folder inside each root folder of python files to make resolve works.
So you mean that I should have to create files for every directory of code that I have in the same project? Large project might have hundreds of folders of code. You mean that I need to create these files for every folder that I want the language server to work in?
It should work like this, since it needs to know the root folder of each file for file resolve, I can't understand what's your problem.
Shouldn't the root be the same for the whole project? Regardless of where code is placed? Please tell me how to set it up to make it work for the really simple project structure example in https://github.com/neoclide/coc-python/issues/26#issuecomment-482017928
You should use relative import.
I am using relative imports. Those are the ones the language server doesn't resolve. The imported files are in the same folder but still not seen by the language server!
I don't know how to explain this better, but if I set up exactly this structure manually (same as in https://github.com/neoclide/coc-python/issues/26#issuecomment-482017928)
project-root
├── .git
└── src
├── directory1
│ ├── file1.py (file-contents: "import file2")
│ └── file2.py (file-contents: "import file1")
└── directory2
├── file1.py (file-contents: "import file2")
└── file2.py (file-contents: "import file1")
I can't get it to work.
I'm using autocmd FileType python let b:coc_root_patterns = ['.git']
and as you can see there is a .git file in the root.
If I run :CocList folders
it's clear that it correctly found the project root
Still, none of the relative imports in any of the files work.
Same issue on VSCode, as I said it use workspaceFolders for resolve python files, which could be wrong.
Do you have any suggestion to improve it?
No, this issue is not present in VSCode. If I open the same project there I get no unresolved imports and things like "go to definition" works.
Have you tried setting up the example project from my previous comment? Maybe it's only in my environment?
I tried, unresolved import with jedi or not:

it should be possible to configure jedi/pylint to fix this problem.
Alright. Apparently my vscode still used jedi when I tested it... Jedi works fine in vscode, the language server does not work.
Sorry for taking your time with this. I guess that this is a MPLS bug so I'll open an issue there.
I guess the short term solution would be to set the workspaceFolder to the same directory that each file is in. Can this be done with coc? Is there a problem with doing this?
I have been able to consistently resolve this issue by setting PYTHONPATH
within a .env
file.
For the following directory structure, it should work if
PYTHONPATH="src/directory1:src/directory2"
is in the file.
project-root ├── .git ├── .env └── src ├── directory1 │ ├── file1.py (import file2) │ └── file2.py (import file1) └── directory2 ├── file1.py (import file2) └── file2.py (import file1)
This placement of the .env
file assumes the default value of python.envFile
.
I think that MPLS should document this better.
To verify this is working, look at CocInfo, you should see a list of Configuration search paths
which correspond.
If you can't make rootPattern to work for resolve workspaceFolder, you can
- Run
:CocList folders
.- Press
<cr>
and edit the path to correct folder.- Run
:CocRestart
to restart service.- Save a session file to persist workspaceFolders.
How would I go about saving the session to a file @chemzqm ?
@joaqo you can run the command :CocCommand session.save
https://github.com/neoclide/coc.nvim/wiki/Using-workspaceFolders
Having a similar issue trying to use the django-stubs plugin with mypy (details in https://github.com/typeddjango/django-stubs/issues/134#issuecomment-638679746).
I did set in my vimrc
autocmd FileType python let b:coc_root_patterns = ['.git', '.env']
and created a .env file with
PYTHONPATH=${PYTHONPATH}:${PWD}:${PWD}/project/
DJANGO_SETTINGS_MODULE="project.settings.dev"
But the plugin still fails, as if it couldn't read the file project/settings/dev.py
Just as an FYI, if you are here because you have the same issue and workspaceFolders doesn't fix your issue, most likely than not, you are using a version manager like asdf
, if so, when you run coc, the interpreter is being selected properly. Please refere to this link to work around it: https://www.reddit.com/r/neovim/comments/dyl6xw/need_help_setting_up_cocnvim_for_python_with/
Just tried every fix for this issue, but I couldn't fix It in python 2.
- Navigate to project directory (which has a .git folder)
- Activate virtualenv (works flawlessly in shell)
- Open file in this same directory
- Got the unresolved import message:
I don't know why It flagged the
import os
, but didn't flagged theimport sys
.
- I did the @stoicAlchemist suggestion, as well as the popular
autocmd FileType python let b:coc_root_patterns = ['.git', '.env']
in my init.vim. - If I run :CocList folders it's clear that it correctly found the project root and CocRestart doesn't change a thing.
- I guess the virtualenv is correctly set when I open neovim, since I'm able to open pythonREPL with Coc without issues.
- I don't think a .env file should be necessary.
checkhealth provider
output:
Update
When I use a vitrualenv created in the command line (via virtualenv), the error occurs and the virtualenv info in the status bar says ('venvname': venv)
.
When I use a virtualenv created in pycharm everything works and the info displayed is ('venvname': virtualenv)
.
Both virtualenvs work fine in REPL.