LinuxCommandLibrary
GitHubF-DroidGoogle Play Store

autoflake

Remove unused imports and variables from Python code

TLDR

Preview changes as a diff without modifying the file
$ autoflake [file.py]
copy
Remove all unused imports in place
$ autoflake --in-place --remove-all-unused-imports [file.py]
copy
Remove unused imports recursively from all Python files
$ autoflake --in-place --remove-all-unused-imports -r [.]
copy
Check without modifying and exit non-zero if changes are needed
$ autoflake --check [file.py]
copy
Remove unused imports and variables in place
$ 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

-i, --in-place

Modify files directly instead of printing a diff
-s, --stdout
Write the result to standard output
--remove-unused-variables
Remove unused variables
--remove-all-unused-imports
Remove all unused imports, not just standard-library ones
--ignore-init-module-imports
Leave imports in _init_.py files untouched
--expand-star-imports
Expand wildcard imports (from x import *) when names can be resolved
--remove-duplicate-keys
Remove duplicate keys in dictionary literals
--check
Exit non-zero if changes are needed, without modifying files (CI mode)
--check-diff
Like --check, but also print the diff of proposed changes
-r, --recursive
Process directories recursively
-j n, --jobs n
Number of parallel jobs (0 uses all CPU cores)
--exclude globs
Exclude files and directories matching the comma-separated globs
--imports modules
Comma-separated list of additional modules whose unused imports should be removed

INSTALL

nix profile install nixpkgs#autoflake
copy

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)

RESOURCES

Braincup
Open source brain training for math, memory and focus
Braincup mini-games
41 mini-games · Apache-2.0
No ads · No tracking
Play in browser
Download Braincup on the App StoreGet Braincup on Google PlayGet Braincup on F-Droid
276 stars
From the maker of Linux Command Library
Copied to clipboard
Braincup
Open source brain training for math, memory and focus. 41 mini-games, from mental arithmetic to Sudoku, N-Back and Solo Chess.
Apache-2.0 licensed · No ads · No tracking · No account
From the maker of Linux Command Library
Download Braincup on the App StoreGet Braincup on Google PlayGet Braincup on F-Droid