Hi, I'm Harlin and welcome to my blog. I write about Python, Alfresco and other cheesy comestibles.

How to Use Source Code Analyzer PyLint

Pylint is a Python source code analyzer that looks for programming errors, helps enforce coding standards and looks for bad smelling code.

Pylint has a lot of rules that are active out of the box. Still, you can configure which ones you don't need by editing a pylintrc file. You can also write your own plugins for it.

Best of all, it's free software distributed under the GNU General Public License.

How to install

Hopefully, you're using pyenv to set up your project environments. If so, then pip should be available. To install it you can run the pip install command:

# pip install pylint

If you don't have pip installed, there are some alternative installation methods you can use.

Using Pylint

To get an idea of how to use it, open an editor and add the following code to a file called script.py:

#!/usr/bin/env python

class Greeter(object):

    def get_name(self, ask_name):
        return raw_input(ask_name)

    def greet(self, name):
        print('Well, hello {} you are well met!').format(name)

    def run(self):
        name = self.get_name('Hi, what is your name?')
        self.greet(name)

if __name__ == '__main__':
    greeter = Greeter()
    greeter.run()

From the command line, run:

# pylint script.py

You should see some output similar to this:

W:  5, 0: Found indentation with tabs instead of spaces (mixed-indentation)
W:  6, 0: Found indentation with tabs instead of spaces (mixed-indentation)
W:  8, 0: Found indentation with tabs instead of spaces (mixed-indentation)
W:  9, 0: Found indentation with tabs instead of spaces (mixed-indentation)
W: 11, 0: Found indentation with tabs instead of spaces (mixed-indentation)
W: 12, 0: Found indentation with tabs instead of spaces (mixed-indentation)
W: 13, 0: Found indentation with tabs instead of spaces (mixed-indentation)
W: 17, 0: Found indentation with tabs instead of spaces (mixed-indentation)
C: 18, 0: Final newline missing (missing-final-newline)
W: 18, 0: Found indentation with tabs instead of spaces (mixed-indentation)
C:  1, 0: Missing module docstring (missing-docstring)
C:  3, 0: Missing class docstring (missing-docstring)
C:  5, 1: Missing method docstring (missing-docstring)
R:  5, 1: Method could be a function (no-self-use)
C:  8, 1: Missing method docstring (missing-docstring)
R:  8, 1: Method could be a function (no-self-use)
C: 11, 1: Missing method docstring (missing-docstring)
C: 17, 1: Invalid constant name "greeter" (invalid-name)
--------------------------------------------------------------------
Your code has been rated at -6.36/10 (previous run: -6.36/10, +0.00)

The list is of "violations" of PEP8 best practice in coding standards. The idea is to fix each of these until you get a 10 of 10.

So, going through each "violation":

Lines 6-17,18 denotes that we are using tabs instead of spaces. Your editor should have a way to convert the tabs to spaces.

Line 18 mentions that we didn't put a newline at the end of the file. To fix, just add a new line to the end of the file.

There are a number of lines that are missing a docstring. You can remedy this by adding something like:

"""
My comment ...
"""

There are a couple of methods that could be written as external functions. Hmm, maybe writing this as a class was not the right way to go. :-)

And for C17, not that "greeter" should be GREETER. So, let's make some corrections. Try to correct each violation and see what you come up with.

After doing this on my own, I found that I no longer really needed a class to implement the greeter functionality.

"""
Script for greeting users.
"""
#!/usr/bin/env python

def get_name(ask_name):
    """
    Returns name as string input by user.
    """
    return raw_input(ask_name)


def greet(name):
    """
    Print out greeting to the name given by user.
    """
    print('Well, hello {} you are well met!').format(name)


def run():
    """
    Runs the Greeter class.
    """
    name = get_name('Hi, what is your name?')
    greet(name)


if __name__ == '__main__':
    run()

Sure, you can use a class here if you prefer and you can ignore everything you want. The pylynt tool really is nothing but a guideline to help you code to standard but know that it's not a must.

If you're interested in getting pylint to ignore certain violation types, you can create a .pylintrc file in your home directory. The format would look something like this when you run:

# pylint --generate-rcfile

This will generate a file like:

[MESSAGES CONTROL]

# Enable the message, report, category or checker with the given id(s). You can
# either give multiple identifier separated by comma (,) or put this option
# multiple time.
#enable=

# Disable the message, report, category or checker with the given id(s). You
# can either give multiple identifier separated by comma (,) or put this option
# multiple time (only on the command line, not in the configuration file where
# it should appear only once).
#disable=

Let's say you would like to ignore violations like "Invalid constant name "greeter" (invalid-name)". You can set disable like so:

disable=invalid-name

You can set multiple directives here but use commas to delimit them.

Now, you should be able to easily ensure your code is being written to PEP8 standard. It will also help you adhere to the DRY principle (Don't Repeat Yourself).

Next time, we will talk about using pylint with a Django project.

Any Comments, Always Welcome!