![]() |
Pylint Quickstart |
Pylint QuickstartThis document is meant to get you started with Pylint. It assumes that you have installed pylint following the instructions in the README document found in the source documentation. 1. What is pylint?Pylint is a tool that checks for errors in python code, and tries to check that a given coding standard is respected by the coders. This is similar but nevertheless different from what pychecker provides, especially since pychecker explicitely does not bother with coding style. The default coding style used by pylint is close to Guido's style guide. Pylint will display a number of errors and warnings as it analyzes the code, as well as some statistics about the number of warnings and errors found in different files. If you run pylint twice, it will display the statistics from the previous run together with the ones from the current run, so that you can see if the code has improved or not. Last but not least, the code is given an overall mark, based on the number an severity of the warnings and errors. This has proven to be very motivating for programmers. 2. Invoking pylintPylint is meant to be called from the commant line. The usage is
pylint [options] module_or_package
You should give pylint the name of a Python package or module. Pylint will import this package or module, so you should pay attention to your PYTHONPATH, since it is a common error to analyze an installed version of a module instead of the development version. It is also possible to analyze python files, with a few restriction. The thing to keep in mind is that pylint will try to convert the file name to a module name, and only be able to process the file if it succeeds.
pylint mymodule.py
should always works since the current working directory is automatically added on top of the python path
pylint directory/mymodule.py
will work if "directory" is a python package (i.e. has an __init__.py file) or if "directory" is in the python path. For more details on this see the FAQ. 3. Pylint outputThe default format for the output is raw text. But passing pylint the --html option will produce an HTML document. There are several sections in pylint's output. 3.1. Source code analysis sectionFor each python module, pylint will first display a few '*' characters followed by the name of the module. Then, a number of messages with the following format:
MESSAGE_TYPE: LINE_NUM:[OBJECT:] MESSAGE
The message type can be:
Sometimes the line of code which caused the error is displayed with a caret pointing to the error. This should be generalized in furure versions of pylint. Example (extracted from a run of pylint on itself...):
************* Module logilab.pylint.checkers.format
W: 50: Too long line (86/80)
W:108: Operator not followed by a space
print >>sys.stderr, 'Unable to match %r', line
^
W:141: Too long line (81/80)
W: 74:searchall: Unreachable code
W:171:FormatChecker.process_tokens: Redefining built-in (type)
W:150:FormatChecker.process_tokens: Too many local variables (20/15)
W:150:FormatChecker.process_tokens: Too many branchs (13/12)
3.2. Metrics sectionThe metrics section displays summaries gathered from the current run.
Finally, a global evaluation for the code is computed, and an optional witty comment is displayed (if --comment=y was specified on the command line). 4. Command line optionsFirst of all, we have two basic (but useful) options.
Pylint is architectured around several checkers. By default all checkers are enabled. You can disable a specific checker by specifying --enable-<checker>=n, or disable all checkers using --disable-all and afterwards enable specific checkers with --enable-<checker>=y. Available checkers are:
basic
checks for :
classes
checks for :
format
checks for :
imports
checks for
metrics
does not check anything but gives some raw metrics :
variables
checks for
miscellaneous
checks for:
Each checker has some specific options, which can take either a yes/no value, an integer, a python regular expression, or a comma separated list of values (which are generally used to override a regular expression in special cases). For a full list of options, use --help Specifying all the options suitable for your setup and coding standards can be tedious, so it is possible to use a rc file to specify the default values. Pylint looks for /etc/pylintrc and ~/.pylintrc. The --generate-rcfile option will generate a commented configuration file according to the current configuration on standard output and exit. You can put other options before this one to use them in the configuration, or start with the default values and hand tune the configuration. Other useful global options include:
5. Bug reportsYou think you have found a bug in Pylint? Well, this may be the case since Pylint is under development. Please take the time to send a bug report to python-projects@logilab.org. This mailing list is also a nice place to discuss Pylint issues. |