The `deadcode` library is a static analysis tool for Python that identifies unused functions, classes, and variables within a codebase. It helps developers clean up projects by pinpointing code that is no longer reachable or imported. As of version 2.4.1, it focuses on providing a robust CLI experience with programmatic API access. The project maintains an active development cycle, with major releases incorporating significant refactors and minor releases for bug fixes and feature enhancements.
Install & Compatibility
Where this runs
tested against v2.4.1 · 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.910 runs
installs and imports cleanly · install 0.0s · import 0.000s · 18.2MB
glibcpy 3.10–3.910 runs
installs and imports cleanly · install 1.8s · import 0.000s · 19MB
17MB installed
● package 17MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
from deadcode import importlib
This quickstart demonstrates how to use the `deadcode` command-line interface (CLI) to scan a temporary Python project for unused code. It creates two dummy Python files, one with a dead function, then executes `deadcode` and prints its output and the generated JSON report. This illustrates the typical workflow of using `deadcode` on a project.
import subprocess
import json
import tempfile
from pathlib import Path
import shutil
# Create a temporary directory to simulate a project
temp_dir = Path(tempfile.mkdtemp())
# Create dummy Python files within the temporary project
# my_module.py contains an unused function
(temp_dir / "my_module.py").write_text("""
def used_func():
return "hello"
def unused_func():
return "world"
""")
# main.py imports and uses 'used_func', leaving 'unused_func' dead
(temp_dir / "main.py").write_text("""
from my_module import used_func
print(used_func())
""")
# Define the output report file path
output_file = temp_dir / "deadcode_report.json"
try:
print(f"Scanning directory: {temp_dir}")
# Run the deadcode CLI tool
result = subprocess.run(
["deadcode", "--path", str(temp_dir), "--output", str(output_file)],
capture_output=True,
text=True,
check=True # Will raise CalledProcessError if command fails
)
print("\n--- CLI STDOUT ---")
print(result.stdout)
if result.stderr:
print("\n--- CLI STDERR ---")
print(result.stderr)
# Check if the report file was generated and print its content
if output_file.exists():
with open(output_file, 'r') as f:
report = json.load(f)
print("\n--- DEAD CODE REPORT ---")
print(json.dumps(report, indent=2))
else:
print("\nNo dead code report file generated.")
except subprocess.CalledProcessError as e:
print(f"\nERROR: deadcode command failed with exit code {e.returncode}")
print(f"Stdout: {e.stdout}")
print(f"Stderr: {e.stderr}")
except FileNotFoundError:
print("\nERROR: 'deadcode' command not found. Please ensure the 'deadcode' package is installed and its executable is in your system's PATH.")
finally:
# Clean up the temporary directory and its contents
if temp_dir.exists():
print(f"\nCleaning up temporary directory: {temp_dir}")
shutil.rmtree(temp_dir)
deadcode --version
Upgrade
Version history
Breaking-change detection hasn't run for this library yet.
Audit
Security & dependencies
CVE tracking and dependency tree are planned for a later release.