Comments (22)
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.
from coc-python.
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.
from coc-python.
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 ?
from coc-python.
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.
from coc-python.
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
from coc-python.
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.
from coc-python.
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?
from coc-python.
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.
from coc-python.
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.
from coc-python.
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.
from coc-python.
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 #26 (comment)
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!
from coc-python.
I don't know how to explain this better, but if I set up exactly this structure manually (same as in #26 (comment))
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.
from coc-python.
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?
from coc-python.
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?
from coc-python.
I tried, unresolved import with jedi or not:
it should be possible to configure jedi/pylint to fix this problem.
from coc-python.
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?
from coc-python.
@joaqo you can run the command :CocCommand session.save
from coc-python.
https://github.com/neoclide/coc.nvim/wiki/Using-workspaceFolders
from coc-python.
Having a similar issue trying to use the django-stubs plugin with mypy (details in typeddjango/django-stubs#134 (comment)).
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
from coc-python.
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/
from coc-python.
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 theimport 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.
from coc-python.
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.
from coc-python.
Related Issues (20)
- yarn install --frozen-lockfile fails HOT 2
- Intellisense low on offers HOT 1
- Cannot disable linting HOT 1
- Wrong Pyright errors. HOT 1
- python.createTerminal not activating virtual env
- iPython as interpreter
- How to make reappear the info of a function
- Why did this happen whenever I opened a python file HOT 1
- "Linter pylint is not installed." Even though it is.
- coc formatter deleting lines when formatting sometimes HOT 1
- Black does not support the "Format Selection" command
- Modified the wrong code, but the underline still exists. HOT 1
- Can't get out of the terminal one interacted with HOT 1
- Uncaught Exception Error on Windows!
- Default ctags exclude list is unconfigurable HOT 1
- Jedi error raised on python completion HOT 2
- Silence [coc.nvim]: Uncaught exception: spawn python2 ENOENT HOT 4
- Autocompletion through Coc vs VS Code HOT 2
- Settings
- Error float window hangs and then not able to get completion HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from coc-python.