Giter Site home page Giter Site logo

Unresolved import about coc-python HOT 22 OPEN

neoclide avatar neoclide commented on June 3, 2024 7
Unresolved import

from coc-python.

Comments (22)

chemzqm avatar chemzqm commented on June 3, 2024 8

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.

rgreenblatt avatar rgreenblatt commented on June 3, 2024 8

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.

joaqo avatar joaqo commented on June 3, 2024 2

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.

chemzqm avatar chemzqm commented on June 3, 2024 1

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.

jberglinds avatar jberglinds commented on June 3, 2024

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.

jberglinds avatar jberglinds commented on June 3, 2024

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.

jberglinds avatar jberglinds commented on June 3, 2024

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.

chemzqm avatar chemzqm commented on June 3, 2024

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.

jberglinds avatar jberglinds commented on June 3, 2024

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.

chemzqm avatar chemzqm commented on June 3, 2024

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.

jberglinds avatar jberglinds commented on June 3, 2024

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.

jberglinds avatar jberglinds commented on June 3, 2024

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.

chemzqm avatar chemzqm commented on June 3, 2024

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.

jberglinds avatar jberglinds commented on June 3, 2024

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.

chemzqm avatar chemzqm commented on June 3, 2024

I tried, unresolved import with jedi or not:

Screen Shot 2019-04-12 at 4 22 10 PM

it should be possible to configure jedi/pylint to fix this problem.

from coc-python.

jberglinds avatar jberglinds commented on June 3, 2024

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.

marco-silva0000 avatar marco-silva0000 commented on June 3, 2024

@joaqo you can run the command :CocCommand session.save

from coc-python.

chemzqm avatar chemzqm commented on June 3, 2024

https://github.com/neoclide/coc.nvim/wiki/Using-workspaceFolders

from coc-python.

AdrienLemaire avatar AdrienLemaire commented on June 3, 2024

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.

stoicAlchemist avatar stoicAlchemist commented on June 3, 2024

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.

thales-maciel avatar thales-maciel commented on June 3, 2024

Just tried every fix for this issue, but I couldn't fix It in python 2.

  1. Navigate to project directory (which has a .git folder)
  2. Activate virtualenv (works flawlessly in shell)
  3. Open file in this same directory
  4. Got the unresolved import message:
    Screenshot_20201120_182337
    I don't know why It flagged the import os, but didn't flagged the import 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:
image

from coc-python.

thales-maciel avatar thales-maciel commented on June 3, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.