From this tutorial, you will be learning about Pylint in Python. It is a static code analysis tool to find coding errors in your Python code.
Note: The syntax used in the below section is for Python 3. You may change it to use with a different version of Python.
Learn about Pylint
Must Read – Best Python IDE
What is Pylint?
It is a static code analysis tool to identify errors in Python code and helps programmers enforce good coding style. This tool enables them debugging complex code with less manual work.
It is one of the tools which gets used for test-driven development (TDD).
The coding style that Pylint applies to the code is known as PEP8.
For more information, about PEP8 visit the link: PEP8 Style Guide for Python
Other products which are similar to Pylint are pyflakes, mypy, etc.
It is a must-have tool for every beginner as well as advanced users as it scans and rates programs with a score according to the rules outlined in the PEP8 Style Guide.
How to install and use Pylint?
To install it on systems such as Windows 10, Mac OS, Linux, use the following command:
pip install pylint
You can also use alternative methods such as:
1. On Debian, Kali Linux, Ubuntu based systems such as Ubuntu, Elementary, etc.
# Debian, Kali Linux, Ubuntu sudo apt install pylint
2. On Fedora
# Fedora sudo dnf install pylint
3. On OpenSUSE
# OpenSUSE sudo zypper install pylint
# OpenSUSE sudo zypper install python3-pylint
You can even integrate pylint into various IDE (Integrated Development Environment) such as Eclipse, Visual Studio Code, etc. However, here, we will focus on pylint usage without using IDE integration.
The command to use pylint on a python file is:
# Check for style errors pylint filename.py
It returns output consisting of semantic errors, syntax errors, errors in coding style, bugs in the code, excessive and redundant code, etc. It also assigns a score that indicates whether the python code is an ideal one to use and maintains a history of scores obtained while running over a python file as well as after each edit.
In the next section, you can check out sample programs demonstrating its usage.
Python program with style issues:
Here is a simple program (sample.py) having some styling issues.
a = 23 b = 45 c = a + b print(c)
Below is the output after you pass the above sample to Pylint. It lists down multiple styling issues in the program.
A better version of the above sample:
After fixing the code, the modified version looks like this:
""" Code to add two numbers """ a = 23 b = 45 c = a + b print(c)
The output will come as: