LinuxCommandLibrary

autoflake

Remove unused imports and variables from Python code

TLDR

Remove unused imports from file

$ autoflake --in-place --remove-unused-variables [file.py]
copy
Remove from all Python files
$ autoflake --in-place --remove-all-unused-imports -r [.]
copy
Check without modifying
$ autoflake --check [file.py]
copy
Remove unused imports and variables
$ autoflake --in-place --remove-unused-variables --remove-all-unused-imports [file.py]
copy

SYNOPSIS

autoflake [--in-place] [--remove-unused-variables] [options] files

DESCRIPTION

autoflake removes unused imports and variables from Python code. It helps keep code clean by eliminating dead code automatically.
The tool uses static analysis to identify unused elements and can modify files in place or report issues.

PARAMETERS

--in-place

Modify files directly
--remove-unused-variables
Remove unused variables
--remove-all-unused-imports
Remove all unused imports
--remove-duplicate-keys
Remove duplicate dictionary keys
--check
Return error if changes needed (CI mode)
-r, --recursive
Process directories recursively
--exclude pattern
Exclude files matching pattern
--imports modules
Additional modules to remove

CAVEATS

May incorrectly identify imports used only in type hints. Does not handle all edge cases. Review changes before committing. Works well with other formatters like black.

HISTORY

autoflake was created to automate cleanup of unused Python imports, complementing code formatters and linters.

SEE ALSO

black(1), isort(1), flake8(1)

> TERMINAL_GEAR

Curated for the Linux community

Copied to clipboard

> TERMINAL_GEAR

Curated for the Linux community