Registry / testing / autoflake

autoflake

JSON →
library2.4.0pypypi✓ verified 25d ago

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.

pip install autoflake
INSTALL
IMPORT
SIG · AUTOFLAKE
A
autoflake
testingpythonv2.4.0
Install
1.7s avg
Import
286ms
Disk
17MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v2.4.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.318s · 18.7MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.7s · import 0.254s · 19MB
17MB installed
● package 17MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

fix_code
from autoflake import fix_code
For programmatic analysis and modification of Python code strings.
fix_file
from autoflake import fix_file
For programmatic analysis and modification of Python files by path.

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.

import os import autoflake # Example file content with unused import and variable example_code = """ import os # Unused import sys def my_func(): x = 10 # Unused y = 20 print(sys.version, y) """ # Fix the code programmatically cleaned_code = autoflake.fix_code( example_code, remove_all_unused_imports=True, remove_unused_variables=True ) print("Original code:\n" + example_code) print("\nCleaned code:\n" + cleaned_code) # --- CLI Usage Example (most common) --- # Create a dummy file for CLI demonstration dummy_file_path = "./temp_autoflake_example.py" with open(dummy_file_path, "w") as f: f.write(example_code) print(f"\nCreated dummy file: {dummy_file_path}") print("Running autoflake via CLI (simulated):") # Simulate CLI command, usually run from shell # import subprocess # subprocess.run(["autoflake", "--in-place", "--remove-all-unused-imports", "--remove-unused-variables", dummy_file_path]) # To see the effect, you'd re-read the file after running the actual CLI command # For this example, we'll just demonstrate the programmatic result. # Clean up the dummy file os.remove(dummy_file_path) print(f"Removed dummy file: {dummy_file_path}")
autoflake --version
Debug
Known issues
breakingautoflake dropped support for Python 3.8 starting from version 2.3.2. Ensure your environment uses Python 3.10 or newer.
fix
Upgrade your Python interpreter to version 3.10 or higher. If you must use Python 3.8, pin autoflake to `<2.3.2`.
affects: >=2.3.2
gotchaautoflake 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.
fix
Ensure your project is under Git or another version control system. Review changes before committing them.
affects: All versions
gotchaautoflake 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.
fix
Always include the specific removal flags relevant to your cleanup task when running autoflake, both from CLI and programmatically.
affects: All versions
gotchaTOML 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.
fix
If using TOML configuration, ensure your autoflake version is 2.3.0 or newer. Otherwise, rely on CLI arguments for configuration.
affects: 2.1.1 to 2.2.x
gotchaautoflake 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.
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.
affects: All versions
Errors
Common errors & fixes
portal/reports.py: Unused imports/variables detected
The 'check' option in the pyproject.toml configuration file is set to true, causing autoflake to only report issues without modifying the files.
fix
Remove the 'check = true' line from the [tool.autoflake] section in pyproject.toml to allow autoflake to make in-place changes.
error: externally-managed-environment × This environment is externally managed ╰─> To install Python packages system-wide, try apt install python3-xyz, where xyz is the package you are trying to install. If you wish to install a non-Debian-packaged Python package, create a virtual environment using python3 -m venv path/to/venv. Then use path/to/venv/bin/python and path/to/venv/bin/pip. Make sure you have python3-full installed. If you wish to install a non-Debian packaged Python application, it may be easiest to use pipx install xyz, which will manage a virtual environment for you. Make sure you have pipx installed. See /usr/share/doc/python3.12/README.venv for more information. note: If you believe this is a mistake, please contact your Python installation or OS distribution provider. You can override this, at the risk of breaking your Python installation or OS, by passing --break-system-packages. hint: See PEP 668 for the detailed specification.
Attempting to install autoflake system-wide in a Debian-based distribution that manages Python packages externally.
fix
Create a virtual environment using 'python3 -m venv path/to/venv', activate it, and then install autoflake within this environment using 'pip install autoflake'.
autoflake does not remove unused global variables
Autoflake is designed to remove unused imports and variables, but it may not remove unused global variables defined outside of functions.
fix
Ensure that the '--remove-unused-variables' option is used when running autoflake. If the issue persists, consider manually reviewing and removing unused global variables.
'autoflake' is not recognized as an internal or external command, operable program or batch file
The 'autoflake' executable is not found in the system's PATH, usually because it wasn't installed in the active Python environment or the environment is not correctly activated.
fix
Ensure 'autoflake' is installed in your active Python environment. If using a virtual environment, activate it before running the command.
`pip install autoflake`
`# If using a virtual environment:`
`source .venv/bin/activate  # Linux/macOS`
`# or .\.venv\Scripts\activate # Windows PowerShell`
`autoflake <arguments>`
autoflake prints unused imports/variables but doesn't remove them
The `--in-place` flag was not provided, which is necessary for autoflake to modify files directly. Alternatively, a configuration file (e.g., `pyproject.toml`) might have `check = true` enabled, making autoflake only report changes without applying them.
fix
Add the `--in-place` flag to your autoflake command to apply changes directly to the files. If using a configuration file, set `check = false` or remove the `check` option.
`autoflake --in-place --remove-all-unused-imports --remove-unused-variables <your_file.py>`
`# In pyproject.toml:`
`[tool.autoflake]`
`check = false`
Upgrade
Version history
2.4.0latest on PyPI · released Aug 22, 2026
Audit
Dependencies

No dependency data recorded yet.

Agent activity
28 hits · last 30 days
node
22
OpenAI (training)
2
Resources
autoflake — pip install autoflake · libregistry