Ale: pylint import problem with modules in the same folder

Created on 7 Dec 2016  Â·  13Comments  Â·  Source: dense-analysis/ale

I'm seeing a weird error being reported by pylint only running through ale.

As a MWE, I create in an empty folder two files, a.py and b.py and I import a in b.py, as shown in the screenshot.

screen shot 2016-12-07 at 22 46 47

As you can see, pylint complains that it cannot import a, but if I run it on the command line there is no problem:

[[temp shell]][22:53]/var/folders/d7/836zgpqn22q8v_tr90hvm9m40000gn/T/tmp.3195.2414.11604.10112$ pylint b.py
************* Module b
C:  3, 0: Final newline missing (missing-final-newline)
C:  1, 0: Missing module docstring (missing-docstring)

I was wondering if this is just me, or maybe it's picking the wrong pylint (I cannot configure the executable path...).

enhancement

Most helpful comment

To leave this here, I found a workaround modifying the init-hook of.pylintrc:

init-hook='import sys; sys.path.append(".")'

However, some general solution would be desirable, of course.

All 13 comments

It can't find module b because it doesn't know where the module is. We should possibly modify the command to include some directory in PYTHONPATH. The a.py text is being copied to a temporary file, and that temporary directory doesn't include b.py.

To leave this here, I found a workaround modifying the init-hook of.pylintrc:

init-hook='import sys; sys.path.append(".")'

However, some general solution would be desirable, of course.

The trick is in giving a good enough guess as to which directories to add to PYTHONPATH by default. Maybe the same directory the file is in and the top most directory with Python files in folders in it.

Using the VIRTUAL_ENV environment variable would handle 100% of my usecases. If this isn't present this would be handled by the system python path lookup anyway.

I guess VIRTUAL_ENV is included already? If I understand properly, the problem is that the current directory (implicitly included in the PYTHONPATH) changes for the linting and therefore the correct path is not copied over.

This is related to https://github.com/maralla/validator.vim/issues/56.

Basically it boils down to the fact that pylint expects to be run in the "correct" folder. I would say creating the tempfile in the same dir of the original file is the only solution, but it's very ugly...

That causes other problems, like leaving temporary files behind and the filename not matching the original file.

True... It's a hard problem.

On Thu, Dec 8, 2016 at 10:08 AM, w0rp notifications@github.com wrote:

That causes other problems, like leaving temporary files behind and the
filename not matching the original file.

—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
https://github.com/w0rp/ale/issues/208#issuecomment-265689839, or mute
the thread
https://github.com/notifications/unsubscribe-auth/AAqA8JpdOEElVVreibyDMaM1rNc6WXAKks5rF8kNgaJpZM4LHK6E
.
{"api_version":"1.0","publisher":{"api_key":"
05dde50f1d1a384dd78767c55493e4bb","name":"GitHub"},"entity":
{"external_key":"github/w0rp/ale","title":"w0rp/ale","subtitle":"GitHub
repository","main_image_url":"https://cloud.githubusercontent.com/assets/
143418/17495839/a5054eac-5d88-11e6-95fc-7290892c7bb5.png","
avatar_image_url":"https://cloud.githubusercontent.com/
assets/143418/15842166/7c72db34-2c0b-11e6-9aed-b52498112777.png
","action":{"name":"Open in GitHub","url":"https://github.com/w0rp/ale
"}},"updates":{"snippets":[{"icon":"PERSON","message":"@w0rp in #208:
That causes other problems, like leaving temporary files behind and the
filename not matching the original file."}],"action":{"name":"View
Issue","url":"https://github.com/w0rp/ale/issues/208#
issuecomment-265689839"}}}

--
Dr. Albert Puig Navarro
SNSF Ambizione Fellow
Physik-Institut @ Universität Zürich
http://cern.ch/albert.puig

Following up on @apuignav's workaround, instead of editing each .pylintrc, adding this to ale_python_pylint_options worked fairly well for me:

let g:ale_python_pylint_options = "--init-hook='import sys; sys.path.append(\".\")'"

It still didn't pick up the relative imports, but it is recognizing modules in the same directory.

I have the same problem with relative imports

from . import mod
from ..pkg import mod2

It can't use PYTHONPATH to solve.

Now the pylint linter will check files on disk instead, so you will have to open files or save them, and import paths will be understood for more projects. The version of pylint from virtualenv directories, as long as they live in some directory above the file you're editing, will be used instead, unless you turn that option off with g:ale_python_pylint_use_global.

We might be able to check pylint files on the fly in the future if pylint get support for working with temporary files or input from stdin. See here: https://github.com/PyCQA/pylint/issues/1187

@w0rp it seems pylint does now have support for stdin. I haven't checked it is sufficient for your needs though I'm afraid.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ilyakopy picture ilyakopy  Â·  4Comments

aressler38 picture aressler38  Â·  3Comments

plexigras picture plexigras  Â·  3Comments

alexlafroscia picture alexlafroscia  Â·  4Comments

trevordmiller picture trevordmiller  Â·  4Comments