Product docs and API reference are now on Akamai TechDocs.
Search product docs.
Search for “” in product docs.
Search API reference.
Search for “” in API reference.
Search Results
 results matching 
 results
No Results
Filters
Installing and Using Pylint for Python 3
Traducciones al EspañolEstamos traduciendo nuestros guías y tutoriales al Español. Es posible que usted esté viendo una traducción generada automáticamente. Estamos trabajando con traductores profesionales para verificar las traducciones de nuestro sitio web. Este proyecto es un trabajo en curso.
What is Pylint?
Pylint is a code analysis tool to identify errors in Python source code. Specifically, Pylint helps programmers improve their code quality and style. The coding style that Pylint uses is PEP8.
Code Quality could include sensible programming logic, correct spelling in comments, more idiomatic Python constructs, variable names that match a particular style guide, etc. Also, developers use Pylint to beautify even a small piece of Python code.
Whatever your situation, it is a must-have tool for a beginner as well as advanced developers as it rates your programs with a score as per the rules outlined in PEP8 Style Guide .
Prerequisites
You need to have
pip
installed. If you have not installedpip
, install using the below command:sudo apt install python-pip
Verify the
pip
installation using theversion
flag.pip --version pip 9.0.1 from /usr/lib/python2.7/dist-packages (python 2.7)
Install Pylint
You can install Pylint on systems like Windows, MacOS, Linux, or another Unix. On executing the below command, the pylint
executable becomes available at the native command line.
pip install pylint
Alternatively, you can also execute the below command:
python -m pip install pylint
The Pylint Site provides several Installation variations for different Operating Systems.
Verify Pylint installation using the version
flag.
pylint --version
pylint 2.7.0
Getting Started with Pylint
To learn what Pylint can do, create an example Python script, and name it
my_sum.py
.- File: my_sum.py
1 2 3 4
sum1 = 3 + 4; print("Sum is %d."; % sum1) Sum is 7.
Pylint can help you be a better, more stylish one, though. From the command line, run:
pylint my_sum.py my_sum.py:1:0: W0301: Unnecessary semicolon
You can also see other diagnostics which we ignore for the moment.
In this case, Pylint helps you conform to the best practices for Python coding as per the PEP8 Style Guide.
Update
my_sum.py
, and re-run Pylint with the unnecessary semicolon removed. Pylint raises its score for the program.- File: my_sum.py
1 2 3
sum1 = 3 + 4 print("Sum is %d." % sum1) Sum is 7.
With Pylint’s help, you have a script with both the correct result and a standard style. Pylint is akin to a spell checker.
Pylint Demonstration
Pylint knows more than mere style. It embeds intelligence about several common coding errors. As a next example, consider the fragment:
- File: my_sum.py
1 2 3 4 5
def my_function(): """ An example for a Pylint demonstration.""" my_sum1 = 3 + 4 print "The sum is %d." % my_sum1 return my_sum
Run the above code in the terminal using Pylint:
username@localhost:~$ pylint my_sum.py
No config file found, using default configuration
************* Module my_sum
...
...
E: 5,11: Undefined variable 'my_sum' (undefined-variable)
--------------------------------------------------------------------
Your code has been rated at -5.00/10 (previous run: -5.00/10, +0.00)
my_sum
is indeed unbound, or unassigned. Maybe the developer intended my_sum1
. In any case, Pylint helped to identify a functional error before the Python code was executed.
Pylint Unreachable Code Demonstration
Update my_sum.py
with the below code:
- File: my_sum.py
1 2 3 4 5 6
def my_function(): """ An example for a Pylint demonstration.""" my_sum1 = 3 + 4 print "The sum is %d." % my_sum1 raise Exception("This shouldn’t happen.") return True
Pylint again complains as follows:
username@localhost:~$ pylint my_sum.py
No config file found, using default configuration
************* Module my_sum
...
...
W: 6, 4: Unreachable code (unreachable)
--------------------------------------------------------------------
Your code has been rated at 4.00/10 (previous run: -5.00/10, +9.00)
Pylint’s right. Once the exception is raised, the control flow passes up and out to an exception handler, and the return result does not affect it. Pylint identifies that the code fragment is syntactically valid, yet it probably represents at least a confusion.
Work Through Pylint Errors
If you are facing thousands of Pylint complaints, for instance, a project’s code has an inconsistent indentation or mixes tabs and spaces for indentation. These kinds of lexical errors are easily corrected, and Pylint’s score soars as soon as they are corrected.
Consider the below example for Variable Checker messages.
- File: variables_demo.py
1 2 3 4 5 6 7 8 9 10
var = 1 def foo(): global v print(v) v = 10 print(v) foo() print(var)
Though the code is clear on what it does, Pylint doesn’t think so.
Pylint expects variable names to be at least three characters long. For a situation like this, at least five remedies are available:
- Comply with its expectations: Change the variable name from
v
to, say,var
- Direct Pylint to ignore this particular name
v
in its judgment - Direct Pylint to accept all names in this particular source
- Direct Pylint to accept all names in all sources of the project; or
- Declare
v
to be a special-purpose name that Pylint accepts for this project.
To follow Pylint’s advice directly, rewrite the above code as follows:
- File: variables_demo.py
1 2 3 4 5 6 7 8 9 10
var = 1 def foo(): global var print(var) var = 10 print(var) foo() print(var)
In that case, you can see that Pylint’s score is improved.
Messages Control
Pylint has an advanced message control feature where you can enable or disable a message either from the command line or from the configuration file.
Alternatively, you can tell Pylint about your own coding conventions. You can write:
- File: example.py
1 2 3 4
def my_func(): """ An experiment with variable names. """ for i in range(20): # pylint: disable=invalid-name print(i)
This has the effect of disabling Pylint’s name-checking for the single variable i
.
To disable Pylint’s name-checking throughout that specific file:
Insert a similar directive, # pylint: disable=invalid-name
, without indentation at the top of the source file.
To configure Pylint throughout an entire project:
Pylint complains about several things. You can create a file that allows you to tell Pylint to ignore certain checks. This file is called .pylintrc
.
Create
pylintrc
file in the directory where you run Pylint—presumably the root or base of the project.pylint --generate-rcfile.
Configure Pylint so that Pylint knows not to complain about your program if your
pylintrc
contains the following:- File: pylintrc
1 2 3 4 5 6 7 8 9 10
## It’s OK to name a loop variable "i" [BASIC] good-names=i ... ... [MESSAGES CONTROL] disable= invalid-name
Note One final tip for your initial encounter with Pylint: if you don’t understand a Pylint report, or don’t agree with it, it’s perfectly fine at least temporarily to direct Pylint to ignore that line with an appropriate# pylint: disable=... directive
.
Is Pylint Worth It?
This introduction emphasizes Pylint’s benefits. Pylint beautifies code, it finds likely errors, and with few exceptions, most practitioners who try Pylint adopt it long-term.
Pylint has tradeoffs, but they are generally small and manageable as:
- It is easy to run Pylint in IDEs and Continuous Integration (CI) pipelines.
- Pylint’s diagnostics are lucid.
- While Pylint takes many minutes on large programs, the time it takes is almost always small in comparison with the errors it spotlights.
If your code in a particular style that Pylint doesn’t like, it might be that your style isn’t widely understood, and this is unfamiliar to other team members.
If Pylint is wrong, and your style deserves acceptance, you can probably write a
pylintrc
directive to work out a compromise.
In any case, consider Pylint as a package deal, and in general, Yes, Pylint is worth it.
Beyond Pylint Basics
Initially, most of your Pylint attention is on the cleanup of source code. Once you are a fluent Pylint user, you are likely to turn your focus to intermediate-level Pylint topics that are beyond the scope of this introduction.
Some of the skills that you can learn:
- How to integrate Pylint in CI and Continuous Testing
- How to configure Pylint to co-operate with other Python tools such as Black , Flake8 , isort , mypy , and so on.
- What should be in
pylintrc
to tune Pylint for large projects. - How to configure Pylint to check for spelling errors.
- How to accommodate actual errors in Pylint.
For now, concentrate on getting to and staying at a quality score of 10, and enjoy the increasingly problem-free source code that Pylint helps you create.
More Information
You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials.
This page was originally published on