Pylint unused-variable, unused-argument, unused-import [Fix]

avatar
Borislav Hadzhiev

Last updated: Apr 10, 2024
4 min

banner

# Table of Contents

  1. Pylint Unused variable 'X' pylint(unused-variable)
  2. Pylint Unused argument 'X' pylint(unused-argument)
  3. Pylint Unused import 'X' pylint(unused-import)

# Pylint Unused variable 'X' pylint(unused-variable)

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.

main.py
def example(): # ⛔️ "site" is not accessed Pylance # ⛔️ Unused variable 'site' pylint(unused-variable) site = 'bobbyhadz.com'

pylint unused variable

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.

main.py
# ✅ No warnings def example(): site = 'bobbyhadz.com' print(site)

You can also resolve the issue by prefixing the variable's name with an underscore.

main.py
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.

main.py
def example(): # pylint: disable-next=unused-variable site = 'bobbyhadz.com'

disable no unused variables pylint warning for single line

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.

main.py
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.

pylintrc
[MESSAGES CONTROL] disable=unused-variable, unused-argument

# Pylint Unused argument 'X' pylint(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.

main.py
# ⛔️ Unused argument 'first_name' pylint(unused-argument) def example(first_name): site = 'bobbyhadz.com' print(site)

unused argument pylint

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.

main.py
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.

main.py
# pylint: disable-next=unused-argument def example(first_name): site = 'bobbyhadz.com' print(site)

disable unused argument rule for single line

Notice that the comment is placed right above the function's definition.

You can also disable the unused-argument rule with the following comment.

main.py
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.

main.py
def example(_first_name): site = 'bobbyhadz.com' print(site)

prefix function parameter with underscore

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.

pylintrc
[MESSAGES CONTROL] disable=unused-variable, unused-argument

# Pylint Unused import 'X' pylint(unused-import)

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.

main.py
# ⛔️ "os" is not accessed Pylance # ⛔️ Unused import os Pylint (unused-import) import os

unused import pylint

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.

main.py
import os print(os) print(os.cpu_count())

Alternatively, you can simply remove the unused import statement.

main.py
# Removed unused import statement print('bobbyhadz.com')

You can also resolve the issue by disabling the unused-import Pylint rule for a line.

main.py
import os # pylint: disable=unused-import print('bobbyhadz.com')

disable unused import rule for line

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.

main.py
# 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.

pylintrc
[MESSAGES CONTROL] disable=unused-import

I've written a detailed guide on how to disable specific Pylint warnings or all warnings in a file.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.