Pylint failing to load plugin on a mercurial precommit hook
Asked Answered
R

3

7

I am trying to create a mercurial pre-commit hook that runs pylint on the pre-commit. My project uses a virtual environment.

I have the hook set up to call pylint on the changed files but I get the error:

Traceback (most recent call last):
    File "/home/barmstrong/.virtualenvs/amp/bin/pylint", line 10, in <module>
        sys.exit(run_pylint())
      File "/home/barmstrong/.virtualenvs/amp/lib/python3.6/site-packages/pylint/__init__.py", line 20, in run_pylint
        Run(sys.argv[1:])
      File "/home/barmstrong/.virtualenvs/amp/lib/python3.6/site-packages/pylint/lint.py", line 1583, in __init__
        linter.load_plugin_modules(plugins)
      File "/home/barmstrong/.virtualenvs/amp/lib/python3.6/site-packages/pylint/lint.py", line 636, in load_plugin_modules
        module = modutils.load_module_from_name(modname)
      File "/home/barmstrong/.virtualenvs/amp/lib/python3.6/site-packages/astroid/modutils.py", line 202, in load_module_from_name
        return load_module_from_modpath(dotted_name.split("."), path, use_sys)
      File "/home/barmstrong/.virtualenvs/amp/lib/python3.6/site-packages/astroid/modutils.py", line 244, in load_module_from_modpath
        mp_file, mp_filename, mp_desc = imp.find_module(part, path)
      File "/usr/lib/python3.6/imp.py", line 297, in find_module
        raise ImportError(_ERR_MSG.format(name), name=name)
    ImportError: No module named 'common'

I believe this is due to a custom plugin in the .pylintrc file that it tries to load from my project directory in:

'/common/blah/file.py'

And in the .pylintrc it is referenced by:

common.blah.file

I try to add this to the PYTHONPATH running:

sys.path.append('path/common')

But the error persists. How do I solve this so it can load my plugin? (I have also tried variations of adding the common module to the PYTHONPATH with no success).

EDIT: If I remove the common.blah/file.py file from my .pylintrc it works, so I need to figure out how I can import it. I have tried adding 'common' to the PYTHONPATH but it does not seem to work.

Rivulet answered 18/10, 2019 at 13:36 Comment(4)
Where do you do the sys.path.append('path/common')? Have you tried actually using the PYTHONPATH environment variable?Gardel
@Gardel I do the sys.path.append at the beginning of the file after imports. I haven't tried actually adding it to PYTHONPATH.Rivulet
At the beginning of which file? If it's the file that you are linting, then it won't help, as it is not actually evaluated. You'll need to set it in the PYTHONPATH env varGardel
Could you show your complete hook?Mayfair
G
0

Unless you're editing the source of pylint, then having

sys.path.append('path/common')

won't be helping at all because pylint isn't evaluating your source files. Even if it were, that would occur after it tried to load your plugin, so it would have already failed.

You'll need to add the path by setting the PYTHONPATH environment variable so that it is available to Pylint when it runs. Don't forget that the path to add is not 'path/common' but 'path/', since it needs to be the path to where the common package lives, not the path of the package itself.

Gardel answered 7/11, 2019 at 21:9 Comment(0)
H
0

I ran into this same issue when I added a plugin to pylint via my .pylintrc:

[MASTER]

load-plugins=path.to.my.plugin

This worked fine on my machine but gave me the ImportError on CircleCI. The solution (building on @lxop's answer) was to run pylint with a custom PYTHONPATH on CircleCI:

PYTHONPATH=$(pwd) pylint
Hachman answered 4/2, 2020 at 14:59 Comment(0)
L
-2

Apparently it's a dependency that you can install in order to resolve your problem, you can find the module here

Longitudinal answered 31/10, 2019 at 9:38 Comment(1)
No, I am pretty sure it is trying to load my custom plugin stored in 'common/pylint/plugin.py' and it cannot find it.Rivulet

© 2022 - 2024 — McMap. All rights reserved.