Last updated: Apr 10, 2024
Reading time·4 min
The Pylint warning "Unused variable 'X' pylint(unused-variable)" is shown when a variable is defined but is not used.
To resolve the issue, use the variable, e.g. by passing it to print()
or
ignore the warning for a single line.
Here is an example of when the warning is shown.
def example(): # ⛔️ "site" is not accessed Pylance # ⛔️ Unused variable 'site' pylint(unused-variable) site = 'bobbyhadz.com'
The site
variable is defined but it wasn't used which caused the issue.
To resolve the issue, use the variable in your code, e.g. by passing it to the print function.
# ✅ No warnings def example(): site = 'bobbyhadz.com' print(site)
You can also resolve the issue by prefixing the variable's name with an underscore.
def example(): _site = 'bobbyhadz.com'
When a variable's name is prefixed with an underscore, we signal to other developers that it is not supposed to be used in public code.
Alternatively, you can disable the Pylint warning for a single line.
def example(): # pylint: disable-next=unused-variable site = 'bobbyhadz.com'
The comment disables the unused-variable
warning for the next line.
There is also a comment that can be placed on the same line as the variable.
def example(): site = 'bobbyhadz.com' # pylint: disable=unused-variable
If you want to disable the unused-variable
rule globally, you have to add it
to the disable
key in your pylintrc
configuration file.
[MESSAGES CONTROL] disable=unused-variable, unused-argument
The Pylint warning "Unused argument 'X' pylint(unused-argument)" is shown when a function or a method argument is not used.
To resolve the issue, make sure to use all function or method arguments in your code or disable the Pylint rule for a single line.
Here is an example of when the warning is shown.
# ⛔️ Unused argument 'first_name' pylint(unused-argument) def example(first_name): site = 'bobbyhadz.com' print(site)
The first_name
argument is not used within the body of the function, so the
warning is shown.
To resolve the issue, make sure to use the argument in the body of the function or method.
def greet(first_name): print(first_name) print(f'hello {first_name}') greet('bobby') # 👉️ hello bobby
Alternatively, you can disable the Pylint rule for a single line.
# pylint: disable-next=unused-argument def example(first_name): site = 'bobbyhadz.com' print(site)
Notice that the comment is placed right above the function's definition.
You can also disable the unused-argument
rule with the following comment.
def example(first_name): # pylint: disable=unused-argument site = 'bobbyhadz.com' print(site)
The warning can also be silenced by prefixing the parameter name with an underscore.
def example(_first_name): site = 'bobbyhadz.com' print(site)
When the name of a variable or a function parameter is prefixed with an underscore, it is an indication to other developers that they shouldn't use it.
These variables or parameters are often used internally by a package and are not supposed to be used publicly by developers who consume the API of the package.
If you want to disable the unused-argument
rule globally, you have to add it
to the disable
key in your pylintrc
configuration file.
[MESSAGES CONTROL] disable=unused-variable, unused-argument
The Pylint warning "Unused import 'X' pylint(unused-import)" is shown when you have imported a module or a variable that is not used in your code.
To resolve the issue, use the imported module, remove the import statement or disable the Pylint rule for a line.
Here is an example of when the warning is shown.
# ⛔️ "os" is not accessed Pylance # ⛔️ Unused import os Pylint (unused-import) import os
We've imported the os module but we haven't used it.
One way to resolve the issue is to use the module in your code, e.g. by calling
a specific method or passing it to the print()
function.
import os print(os) print(os.cpu_count())
Alternatively, you can simply remove the unused import statement.
# Removed unused import statement print('bobbyhadz.com')
You can also resolve the issue by disabling the unused-import
Pylint rule for
a line.
import os # pylint: disable=unused-import print('bobbyhadz.com')
The comment disables the unused-import
rule and resolves the issue even though
the imported module is not used.
You can also use the following comment to achieve the same result.
# pylint: disable-next=unused-import import os print('bobbyhadz.com')
If you want to disable the unused-import
rule globally, you have to add it to
the disable
key in your pylintrc
configuration file.
[MESSAGES CONTROL] disable=unused-import
I've written a detailed guide on how to disable specific Pylint warnings or all warnings in a file.
You can learn more about the related topics by checking out the following tutorials: