neoclide / coc-python

Python extension for coc.nvim, fork of vscode-python
574 stars 51 forks source link

Pylint linting reports false import errors #62

Closed MadWombat closed 5 years ago

MadWombat commented 5 years ago

I am trying to work on a Django project. Django requires some special treatment for imports. When I load a file, I get a bunch of "E0401 import-error" errors from pylint. The problem is, I have pylint configured with a plugin for Django. If I run pylint from command line without any flags, I do not get the import errors. I have tried specifying path to pylint to make sure I am running correct version and loading the django plugin using CLI flags with python.linting.pylintArgs. No matter what, I am still seeing all the import-errors when in neovim, but not when using pylint directly. Any suggestions?

chemzqm commented 5 years ago

You have to make sure selected python interpreter can load pylint with that plugin

MadWombat commented 5 years ago

I believe I have. I am using pyenv, so I set my python interpreter using CocCommand to ~/.pyenv/shims/python location, set python.linting.pylintPath to ~/.pyenv/shims/pylint and checked that when I run pylint from command line I get the same one. It seems to work on CLI, but not from nvim.

I have also managed to capture the command used by coc-python from process list (pylint takes a while on larger files) and tried the exact command from the CLI. On CLI it gives me reasonable output, but in nvim it results in a bunch of import errors.

Note: I replaced full paths with the ~ in the above for brevity, but both the configuration and the commands in the process list use full path to my home dir.

MadWombat commented 5 years ago

This is really confusing. I have tried to run pylint from inside nvim using :!. It works as expected. I disabled every other plugin I had besides coc. I have deleted pylint cache. I have removed my pylint config completely and removed all flags except load plugin for django. I have checked every setting I can think of. Any other way I run it, on the same file, pylint returns a few refactors. But when coc-python runs it I get every Django import marked as bad. Not sure how to debug this further.

MadWombat commented 5 years ago

After a bit more debugging, it seems that the reason I get import errors is because pylint is not beig executed from the directory my vim session is in, but either from the directory where the file being linted resides or from some other location. So any local module imports fail because everything is expected to be loaded from the top project dir. Like if I have the following structure

and in app2.models I do from app1.models import MyModel it will report an import error in the linter. And this type of thing is very common in Django apps.

This is why I always start my vim session in the project root. I wonder if there is some setting I can use to change this behavior and run the linter from the location where my session is rooted (the location vim reports when I use :pwd)

MadWombat commented 5 years ago

I have created a shell script wrapper around pylint, so it reports the current directory before running the linter. The directory it reported was ~/.config/nvim. It uses full path to the file, but this really doesn't work for Django projects or, I suppose, for many other projects that import local modules without modifying sys.path.

chemzqm commented 5 years ago

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

MadWombat commented 5 years ago

Seems like this is a duplicate of #26

MadWombat commented 5 years ago

I was able to get linting to work by creating a .env file at the project root and editing the coc_root_patterns definition to include .env. Thanks for the help.

choucavalier commented 4 years ago

@MadWombat I have the same problem. Could you describe the content of your .env file a little bit further? Thanks