Search code examples
pythonflake8

Unable to setup custom flake8 plugin “FailedToLoadPlugin”


I tried to follow the tutorial, Building a custom Flake8 plugin \| Dunderdoc, to learn building a Flake8 plugin.

After completing the tutorial, I ended up with setup and checker files as below:

setup.py

import setuptools

setuptools.setup(
   name='flake8-picky',
   license='MIT',
   version='0.0.1',
   description='Plugin to comply with my picky standards',
   author='Valdir Stumm Junior',
   author_email='stummjr@gmail.com',
   url='http://github.com/stummjr/flake8-picky',
   py_modules=['flake8_picky'],
   entry_points={
       'flake8.extension': [
           'PCK0 = picky_checker:PickyChecker',
       ],
   },
   install_requires=['flake8'],
   classifiers=[
       'Topic :: Software Development :: Quality Assurance',
   ],
)

picky_checker.py

import ast


class ForbiddenFunctionsFinder(ast.NodeVisitor):
    forbidden = ['map', 'filter']

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.issues = []

    def visit_Call(self, node):
        if not isinstance(node.func, ast.Name):
            return

        if node.func.id in self.forbidden:
            msg = "PCK01 Please don't use {}()".format(node.func.id)
            self.issues.append((node.lineno, node.col_offset, msg))



class PickyChecker(object):
    options = None
    name = 'picky_checker'
    version = '0.1'

    def __init__(self, tree, filename):
        self.tree = tree
        self.filename = filename

    def run(self):
        parser = ForbiddenFunctionsFinder()
        parser.visit(self.tree)

        for lineno, column, msg in parser.issues:
            yield (lineno, column, msg, PickyChecker)

example.py

data = list(range(100))

x = map(lambda x: 2 * x, data)
print(x)

y = filter(lambda x: x % 2 == 0, data)
print(y)

After completing installing plugin successfully, I ran command flake8 example.py and got the following error:

flake8.exceptions.FailedToLoadPlugin: Flake8 failed to load plugin "PCK0" due to No module named picky_checker.

What is that of error and how can I fix that? Thanks


Solution

  • your setup.py is malformed and does not include your package of choice

        py_modules=['flake8_picky'],
    

    should be

        py_modules['picky_checker'],
    

    without that, setuptools will not include your module in the resulting package that gets built/installed


    as an aside, it's best practice to match your module name with your package (though of course not required) -- so I would instead rename your module to flake8_picky.py instead of changing setup.py)