Repolode
PyCQA

PyCQA/autoflake

A command-line tool that removes unused imports and unused variables from Python code, using pyflakes to find them.

Stars
953
Forks
100
Open issues
54
Last commit
2 days ago

What is it?

autoflake cleans up Python source files by deleting imports and variables that pyflakes reports as unused. By default it only removes unused standard library imports, since other modules may have side effects that make automatic removal unsafe. Removing unused variables and wildcard-import expansion are opt-in.

Beyond unused imports, it can remove useless pass statements and duplicate dictionary keys. It can run in place on files or directories, print diffs, or act as a check that returns an error code when changes are needed. Settings can be stored in pyproject.toml or setup.cfg, and a pre-commit hook is available.

Lines marked with a noqa comment are left untouched, and a file can be skipped entirely with a skip_file comment at its top.

Key features

  • 01Removes unused imports, limited to the standard library by default.
  • 02Can remove unused variables with an opt-in flag.
  • 03Supports in-place editing, diff output, and check mode for CI.
  • 04Removes useless pass statements and duplicate dictionary keys.
  • 05Configurable through pyproject.toml or setup.cfg and integrates with pre-commit.

Use cases

  • Cleaning up leftover imports before committing Python code.
  • Enforcing unused-import checks in a CI pipeline.
  • Bulk-cleaning a large legacy Python codebase.
  • Removing imports from third-party modules that are safe to drop, via the --imports option.

Quick start

  1. 1

    Install autoflake from PyPI

    pip install --upgrade autoflake
  2. 2

    Remove unused imports and variables in place from a file

    autoflake --in-place --remove-unused-variables example.py

Things to know

  • By default it only removes unused standard library imports; other modules need the --imports option.

In collections

Topics

FAQ

What is autoflake?
A command-line tool that removes unused imports and unused variables from Python code, using pyflakes to find them. autoflake cleans up Python source files by deleting imports and variables that pyflakes reports as unused. By default it only removes unused standard library imports, since other modules may have side effects that make automatic removal unsafe. Removing unused variables and wildcard-import expansion are opt-in.
Is autoflake free to use? What license does it have?
autoflake is open source under the MIT license. Check the license terms before using it in commercial products.
Is autoflake still maintained?
Actively maintained. The last commit to autoflake was on Nov 4, 2026.
What can I use autoflake for?
Cleaning up leftover imports before committing Python code. Enforcing unused-import checks in a CI pipeline. Bulk-cleaning a large legacy Python codebase. Removing imports from third-party modules that are safe to drop, via the --imports option.
How do I install autoflake?
Install autoflake from PyPI: pip install --upgrade autoflake Remove unused imports and variables in place from a file: autoflake --in-place --remove-unused-variables example.py
Where can I get autoflake?
The source code and installation instructions are on GitHub: https://github.com/PyCQA/autoflake

Summary generated by AI from the project's README. GitHub stats refresh daily.

Related projects