autoflake
Removes unused imports and unused variables
What it is and what it does
autoflake is a command-line tool that automatically detects and removes unused imports and variables from Python source files. It uses pyflakes to identify dead code and by default only removes unused imports from the standard library, leaving third-party imports untouched unless explicitly configured. This conservative default prevents accidental removal of imports that may have side effects.
The tool is typically used as a standalone formatter or integrated into pre-commit hooks to enforce code cleanliness. It supports configuration via pyproject.toml or setup.cfg, can operate on single files or recursively through directories, and offers fine-grained control over what gets removed—from all unused imports to unused variables to duplicate dictionary keys. It also removes useless pass statements by default.
Use it for:
- Run as a pre-commit hook to automatically clean up unused imports before code review.
- Integrate into CI/CD pipelines to check for unused code and fail builds when cleanup is needed.
- Refactor legacy Python codebases to remove accumulated dead imports and variables.
- Configure with --imports to safely remove unused third-party imports in specific projects.
- Use --expand-star-imports to replace wildcard imports with explicit names for clarity.
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
autoflake removes unused imports and unused variables from Python code by analyzing it with pyflakes, and optionally removes useless pass statements.
Yes. autoflake is a lightweight, actively maintained tool with no security vulnerabilities, minimal dependencies, and a permissive MIT license. It solves a real code-quality problem with low friction and is well-suited for both individual developers and teams integrating it into CI/CD workflows.
Install
autoflake on PyPI
pip
pip install autoflakeuv
uv add autoflakepoetry
poetry add autoflakeInstalling autoflake
Before you install
Low install friction with only 2 runtime dependencies (pyflakes and tomli). The project is actively maintained with a recent release on 2026-02-20 and steady commit activity.
License in practice
MIT license is permissive, allowing use in commercial and private projects with minimal restrictions.
Quickstart
pip install autoflake
autoflake --in-place --remove-unused-variables example.py
Requires Python >=3.10.
Verify before relying
- Whether the tool safely handles imports with side effects beyond the documented --imports and # noqa mechanisms.
- Performance characteristics when processing large codebases or using parallel jobs (-j flag).
Package facts
| License | MIT (permissive) |
| Python support | supports the current Python release (>=3.10) |
| Install friction | low — pure-Python wheel |
| Runtime dependencies | 2 — pyflakes, tomli |
| Maintenance | actively maintained — 175 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 6,494,194/month — #1,900 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: autoflake-2.3.3-py3-none-any.whl
Keywords: automatic, clean, fix, import, unused
Tags
More Quality Assurance packages
Coverage.py measures which lines of Python code…
permissive · top 1,000 on PyPI
ruffRuff is a Python linter and code formatter…
permissive · top 1,000 on PyPI
pexpectPexpect spawns and controls interactive console…
permissive · top 1,000 on PyPI
blackBlack reformats Python source code to a…
permissive · top 1,000 on PyPI
pytest-xdistpytest-xdist distributes pytest tests across…
permissive · top 1,000 on PyPI
cfn-lintValidates AWS CloudFormation templates in YAML…
permissive · top 1,000 on PyPI
pyclnPycln finds and removes unused import…
permissive · top 15,000 on PyPI
pyflakesPyflakes checks Python source files for logical…
permissive · top 1,000 on PyPI
yesqayesqa automatically removes unnecessary `#…
permissive · top 15,000 on PyPI
deadcodeScans Python codebases to find and optionally…
agpl · top 15,000 on PyPI
pip-autoremoveA command-line tool that uninstalls a package…
permissive · top 15,000 on PyPI
vultureVulture is a static code analyzer that finds…
permissive · top 5,000 on PyPI
autopep8autopep8 automatically reformats Python code to…
permissive · top 5,000 on PyPI
creosoteCreosote scans your Python project's source…
permissive · top 5,000 on PyPI
pip-check-reqsAudits Python project imports against…
permissive · top 15,000 on PyPI
uncalledDetects unused functions in Python projects…
permissive · top 15,000 on PyPI