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
muslpy 3.10–3.95 runs
installs and imports cleanly · install 0.0s · import 0.318s · 18.7MB
glibcpy 3.10–3.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
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.
fixRemove 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.
fixCreate 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.
fixEnsure 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.
fixEnsure '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.
fixAdd 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.