{"id":1338,"library":"autoflake","title":"autoflake","description":"autoflake is a Python library and command-line tool that removes unused imports and unused variables from Python code. It aims to clean up source files by making minimal, focused changes. It is actively maintained by the PyCQA organization, with frequent minor releases, currently at version 2.3.3.","status":"active","version":"2.3.3","language":"en","source_language":"en","source_url":"https://github.com/PyCQA/autoflake","tags":["code-quality","linter","formatter","refactoring","static-analysis"],"install":[{"cmd":"pip install autoflake","lang":"bash","label":"Install latest version"}],"dependencies":[],"imports":[{"note":"For programmatic analysis and modification of Python code strings.","symbol":"fix_code","correct":"from autoflake import fix_code"},{"note":"For programmatic analysis and modification of Python files by path.","symbol":"fix_file","correct":"from autoflake import fix_file"}],"quickstart":{"code":"import os\nimport autoflake\n\n# Example file content with unused import and variable\nexample_code = \"\"\"\nimport os # Unused\nimport sys\n\ndef my_func():\n    x = 10 # Unused\n    y = 20\n    print(sys.version, y)\n\"\"\"\n\n# Fix the code programmatically\ncleaned_code = autoflake.fix_code(\n    example_code,\n    remove_all_unused_imports=True,\n    remove_unused_variables=True\n)\n\nprint(\"Original code:\\n\" + example_code)\nprint(\"\\nCleaned code:\\n\" + cleaned_code)\n\n# --- CLI Usage Example (most common) ---\n# Create a dummy file for CLI demonstration\ndummy_file_path = \"./temp_autoflake_example.py\"\nwith open(dummy_file_path, \"w\") as f:\n    f.write(example_code)\n\nprint(f\"\\nCreated dummy file: {dummy_file_path}\")\nprint(\"Running autoflake via CLI (simulated):\")\n\n# Simulate CLI command, usually run from shell\n# import subprocess\n# subprocess.run([\"autoflake\", \"--in-place\", \"--remove-all-unused-imports\", \"--remove-unused-variables\", dummy_file_path])\n\n# To see the effect, you'd re-read the file after running the actual CLI command\n# For this example, we'll just demonstrate the programmatic result.\n\n# Clean up the dummy file\nos.remove(dummy_file_path)\nprint(f\"Removed dummy file: {dummy_file_path}\")","lang":"python","description":"The most common way to use autoflake is as a command-line tool. The quickstart shows programmatic usage with `autoflake.fix_code` and demonstrates the core flags. For CLI, `autoflake --in-place --remove-all-unused-imports --remove-unused-variables your_file.py` is typical."},"warnings":[{"fix":"Upgrade your Python interpreter to version 3.10 or higher. If you must use Python 3.8, pin autoflake to `<2.3.2`.","message":"autoflake dropped support for Python 3.8 starting from version 2.3.2. Ensure your environment uses Python 3.10 or newer.","severity":"breaking","affected_versions":">=2.3.2"},{"fix":"Ensure your project is under Git or another version control system. Review changes before committing them.","message":"autoflake modifies files in-place when using the `--in-place` (or `-i`) flag in CLI. Always use it with version control to prevent accidental data loss.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Always include the specific removal flags relevant to your cleanup task when running autoflake, both from CLI and programmatically.","message":"autoflake requires explicit flags to remove unused imports and variables (e.g., `--remove-all-unused-imports`, `--remove-unused-variables`). Without these, it might not perform any changes.","severity":"gotcha","affected_versions":"All versions"},{"fix":"If using TOML configuration, ensure your autoflake version is 2.3.0 or newer. Otherwise, rely on CLI arguments for configuration.","message":"TOML configuration support (`--config` flag) was introduced in v2.1.0, then reverted in v2.1.1, and re-introduced in v2.3.0. Using TOML config in versions 2.1.1 through 2.2.x will not work.","severity":"gotcha","affected_versions":"2.1.1 to 2.2.x"},{"fix":"Combine autoflake with a separate code formatter (e.g., Black) in your pre-commit hooks or CI/CD pipeline for comprehensive code quality enforcement.","message":"autoflake is a linter focused on removing unused code elements; it is not a code formatter like Black or yapf. It will not reformat code style.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}