How to Use Pylint

What you’ll build or solve

You’ll run Pylint on your Python code, read the results, and fix the highest-impact issues first.

When this approach works best

Pylint works best when you want to:

  • Catch common bugs early, like unused variables, missing imports, or unreachable code.
  • Enforce consistent style across a team, so code reviews focus on logic instead of formatting.
  • Improve readability in a growing codebase, like a package with multiple modules.

This is a bad idea when you want a formatter that rewrites code for you. Pylint reports issues, it does not format your files.

Prerequisites

  • Python 3 installed
  • Pylint installed in the environment you use for the project

If you need to install it:

python-m pip install pylint

Step-by-step instructions

1) Run Pylint on a file or package

Start with a single file, then expand to a folder or package.

Option A: Lint one file

pylint app.py

Option B: Lint a package or folder

pylint my_package

What to look for: If you get an import error for your own modules, run Pylint from the project root so imports resolve correctly.


2) Read the output and prioritize fixes

Pylint prints messages with a code and a category. Use those to decide what to fix first.

You will usually see:

  • E for errors (likely bugs)
  • W for warnings (often suspicious code)
  • C for conventions (style consistency)
  • R for refactor suggestions

Fix E and W first, then handle the rest when you have time.

What to look for: Message codes like W0612 help you track a rule or disable it later in a controlled way.

Example output:

app.py:3:4: W0612: Unused variable 'x' (unused-variable)

3) Configure Pylint for your project

Use a config file so everyone runs the same rules.

Generate a starter config:

pylint--generate-rcfile > .pylintrc

Then run Pylint again and iterate:

pylint my_package

What to look for: Keep config changes small. Disable a rule only when you understand why it does not fit your project.


Examples you can copy

Example 1: Fix an unused variable

# app.py
deftotal(items):
x=0
returnsum(items)

Run:

pylint app.py

Fix:

deftotal(items):
returnsum(items)

Example 2: Fix a shadowed built-in name

# bad.py
list= [1,2,3]
print(list)

Fix:

nums= [1,2,3]
print(nums)

Avoid naming variables after built-ins like list, dict, or str.


Example 3: Add docstrings where Pylint expects them

# utils.py
defslugify(text):
returntext.strip().lower().replace(" ","-")

Fix:

defslugify(text):
"""Convert text to a simple slug."""
returntext.strip().lower().replace(" ","-")

Example 4: Run Pylint on a package from the project root

cd my-project
pylint my_package

Running from the project root helps Pylint resolve imports correctly.


Example 5: Disable one message for one line

Use this when a warning is correct in general, but not for a specific line.

importjson

data=json.loads('{"a": 1}')# pylint: disable=consider-using-with

Example 6: Disable messages in .pylintrc

Add message codes when you want them off for the whole project.

[MESSAGES CONTROL]
disable=
    C0114,
    C0115,
    C0116

These codes often relate to missing module, class, or function docstrings.


Common mistakes and how to fix them

Mistake 1: Running Pylint from the wrong folder

What you might do

pylint my_package/utils.py

…from a directory where my_package is not importable.

Why it breaks

Pylint uses Python’s import rules. If your project root is not on the path, imports fail.

Corrected approach

Run Pylint from the project root:

cd my-project
pylint my_package

Mistake 2: Disabling lots of rules to “get a 10/10”

What you might do

Disable many checks until the score looks good.

Why it breaks

You hide useful warnings and lose the point of linting.

Corrected approach

Fix errors and warnings first, then adjust a small set of rules that truly do not fit your codebase.


Troubleshooting

If you see pylint: command not found, run it through Python:

python-m pylint app.py

If you see import-error for your own modules, run Pylint from the project root so imports resolve correctly.

If Pylint feels too slow, lint a smaller target while coding, like one module, then lint the full package before merging changes.

If you get many docstring warnings, decide on a docstring rule for your project and configure it in .pylintrc.

If results differ across machines, confirm everyone uses the same Pylint version:

pylint--version

Quick recap

  • Run pylint on a file or package from the project root.
  • Fix E and W messages first, then handle style and refactor suggestions.
  • Generate a .pylintrc and keep changes small and intentional.
  • Use message codes to disable rules only when you have a clear reason.