python – PyLint Unable to import error – how to set PYTHONPATH?

python – PyLint Unable to import error – how to set PYTHONPATH?

There are two options Im aware of.

One, change the PYTHONPATH environment variable to include the directory above your module.

Alternatively, edit ~/.pylintrc to include the directory above your module, like this:

[MASTER]
init-hook=import sys; sys.path.append(/path/to/root)

(Or in other version of pylint, the init-hook requires you to change [General] to [MASTER])

Both of these options ought to work.

Hope that helps.

The solution to alter path in init-hook is good, but I dislike the fact that I had to add absolute path there, as result I can not share this pylintrc file among the developers of the project. This solution using relative path to pylintrc file works better for me:

[MASTER]
init-hook=from pylint.config import find_pylintrc; import os, sys; sys.path.append(os.path.dirname(find_pylintrc()))

Note that pylint.config.PYLINTRC also exists and has the same value as find_pylintrc().

python – PyLint Unable to import error – how to set PYTHONPATH?

The problem can be solved by configuring pylint path under venv:
$ cat .vscode/settings.json

{
    python.pythonPath: venv/bin/python,
    python.linting.pylintPath: venv/bin/pylint
}

Leave a Reply

Your email address will not be published. Required fields are marked *