autoflake

Autoflake: Remove unused imports and variables in Python code. Clean your Python scripts efficiently with this powerful command-line tool.

Autoflake

What is Autoflake?

Autoflake is a powerful command-line utility designed to automatically remove unused imports and variables from your Python code. It helps maintain clean, efficient, and readable Python scripts by identifying and eliminating dead code that can clutter your projects and potentially introduce subtle bugs. By integrating Autoflake into your development workflow, you can ensure your codebase remains tidy and adheres to best practices.

Key Features and Usage

Autoflake offers several options to customize its behavior. The primary function is to remove unused imports and variables. Here are some common usage examples:

Basic Usage: Removing Unused Imports and Variables

To remove all unused imports and variables in a Python file, use the --in-place flag to modify the file directly:

# Remove all unused imports and variables in a Python file
autoflake --in-place --remove-unused-variables script.py

Removing Only Unused Imports

If you wish to keep unused variables but still clean up imports, you can omit the --remove-unused-variables flag:

# Remove unused imports only (keep unused variables) in a Python file
autoflake --in-place script.py

Recursive Directory Processing

Autoflake can process multiple Python files recursively within a directory:

# Remove unused imports and variables in multiple Python files recursively in a directory
autoflake --in-place --remove-unused-variables --recursive directory_path

Dry Run: Previewing Changes

To see what changes Autoflake would make without actually modifying the files, you can omit the --in-place flag:

# Remove unused imports and variables in a Python file and display the changes without modifying the file
autoflake --remove-unused-variables script.py

Ignoring Specific Modules

You can instruct Autoflake to ignore certain modules from being removed, even if they are unused. This is useful for modules that might be conditionally imported or used in ways Autoflake cannot detect.

# Remove unused imports while ignoring the 'os' module
autoflake --in-place --ignore-init-module-imports=os script.py

Line Length Formatting

Autoflake can also reformat lines while removing unused imports, respecting a specified maximum line length:

# Specify max line length when reformatting lines while removing unused imports
autoflake --in-place --remove-unused-variables --max-line-length=120 script.py

Integrating Autoflake with Other Tools

Autoflake can be effectively combined with other command-line utilities for more advanced workflows.

Using with find

Process all Python files within a directory structure using find:

# Combine with 'find' to process multiple files in different subdirectories
find . -name "*.py" | xargs autoflake --in-place --remove-unused-variables

Using with git

Clean up Python files that were modified in the last commit:

# Combine with 'git' to clean up Python files changed in the last commit
autoflake --in-place --remove-unused-variables $(git diff --name-only HEAD | grep '.py')

Benefits of Using Autoflake

  • Code Cleanliness: Automatically removes dead code, making your Python projects cleaner.
  • Improved Readability: Reduces clutter, making code easier to understand and maintain.
  • Reduced Complexity: Eliminates unused imports and variables that could lead to confusion or errors.
  • Workflow Integration: Easily integrates into CI/CD pipelines and pre-commit hooks.

Further Resources