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 a different version of Python.
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 styles. This tool enables them to debug complex code with less manual work.
It is one of the tools that 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. 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, and Linux, use the following command:
pip install pylint
You can also use alternative methods such as:
1. On Debian, Kali Linux, and 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.
Here is simple Python code where we ran Pytlint. In the output, you can see the -10 rating and find the suggestions. After addressing the issues, the final code is clean and rated 10/10.
Python program with style issues:
Here is a simple program (sample.py) that has 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 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: