{"id":9649,"library":"deadcode","title":"Dead Code Finder","description":"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.","status":"active","version":"2.4.1","language":"en","source_language":"en","source_url":"https://github.com/deadcode-io/deadcode","tags":["code analysis","static analysis","dead code","linting","code quality"],"install":[{"cmd":"pip install deadcode","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Logging functionality","package":"loguru"},{"reason":"Building the command-line interface","package":"typer[all]"},{"reason":"Rich terminal output","package":"rich"},{"reason":"Data validation and settings management","package":"pydantic"},{"reason":"YAML configuration parsing","package":"pyyaml"},{"reason":"TOML configuration parsing","package":"toml"},{"reason":"Code complexity and analysis","package":"radon"},{"reason":"Git repository interaction (e.g., ignoring untracked files)","package":"dulwich"}],"imports":[{"note":"Main function for programmatic dead code detection.","symbol":"find_dead_code","correct":"from deadcode.finder import find_dead_code"},{"note":"Provides a default configuration object for programmatic use.","symbol":"default_config","correct":"from deadcode.config import default_config"}],"quickstart":{"code":"import subprocess\nimport json\nimport tempfile\nfrom pathlib import Path\nimport shutil\n\n# Create a temporary directory to simulate a project\ntemp_dir = Path(tempfile.mkdtemp())\n\n# Create dummy Python files within the temporary project\n# my_module.py contains an unused function\n(temp_dir / \"my_module.py\").write_text(\"\"\"\ndef used_func():\n    return \"hello\"\n\ndef unused_func():\n    return \"world\"\n\"\"\")\n\n# main.py imports and uses 'used_func', leaving 'unused_func' dead\n(temp_dir / \"main.py\").write_text(\"\"\"\nfrom my_module import used_func\nprint(used_func())\n\"\"\")\n\n# Define the output report file path\noutput_file = temp_dir / \"deadcode_report.json\"\n\ntry:\n    print(f\"Scanning directory: {temp_dir}\")\n    # Run the deadcode CLI tool\n    result = subprocess.run(\n        [\"deadcode\", \"--path\", str(temp_dir), \"--output\", str(output_file)],\n        capture_output=True,\n        text=True,\n        check=True # Will raise CalledProcessError if command fails\n    )\n\n    print(\"\\n--- CLI STDOUT ---\")\n    print(result.stdout)\n    if result.stderr:\n        print(\"\\n--- CLI STDERR ---\")\n        print(result.stderr)\n\n    # Check if the report file was generated and print its content\n    if output_file.exists():\n        with open(output_file, 'r') as f:\n            report = json.load(f)\n        print(\"\\n--- DEAD CODE REPORT ---\")\n        print(json.dumps(report, indent=2))\n    else:\n        print(\"\\nNo dead code report file generated.\")\n\nexcept subprocess.CalledProcessError as e:\n    print(f\"\\nERROR: deadcode command failed with exit code {e.returncode}\")\n    print(f\"Stdout: {e.stdout}\")\n    print(f\"Stderr: {e.stderr}\")\nexcept FileNotFoundError:\n    print(\"\\nERROR: 'deadcode' command not found. Please ensure the 'deadcode' package is installed and its executable is in your system's PATH.\")\nfinally:\n    # Clean up the temporary directory and its contents\n    if temp_dir.exists():\n        print(f\"\\nCleaning up temporary directory: {temp_dir}\")\n        shutil.rmtree(temp_dir)\n","lang":"python","description":"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."},"warnings":[{"fix":"Review the official changelog and documentation for 2.x versions. The configuration file was changed from `pyproject.toml` (for `[tool.deadcode]`) to a dedicated `deadcode.toml`.","message":"Version 2.0.0 introduced significant breaking changes, including a major refactor of the internal parsing logic, CLI arguments, and configuration. If upgrading from pre-2.0.0 versions, direct code and config might break.","severity":"breaking","affected_versions":"<2.0.0"},{"fix":"Replace usage of `--exclude-dirs` with `--exclude-patterns` in CLI calls, or update programmatic calls to use `config.exclude_patterns` for directory exclusion (e.g., `['**/tests/', '**/docs/']`).","message":"The `exclude_dirs` argument was removed from both the CLI and programmatic `find_dead_code` function in version 2.0.0. Its functionality has been merged into `exclude_patterns`.","severity":"breaking","affected_versions":"<2.0.0"},{"fix":"Carefully review the reports. Use the `--exclude-patterns` option in `deadcode.toml` or via CLI arguments to fine-tune analysis for specific project structures, dynamically generated files, or known false positives.","message":"Static analysis tools like `deadcode` may produce false positives or negatives, especially with highly dynamic code, complex import mechanisms (e.g., `__import__`, `importlib`), or when code is generated at runtime.","severity":"gotcha","affected_versions":"All"}],"env_vars":null,"last_verified":"2026-04-17T00:00:00.000Z","next_check":"2026-07-16T00:00:00.000Z","problems":[{"fix":"Ensure `pip install deadcode` completed without errors. If installed in a virtual environment, activate it. If using `pipx`, ensure `pipx ensurepath` has been run.","cause":"The `deadcode` executable is not in your system's PATH, or the package was not installed correctly.","error":"deadcode: command not found"},{"fix":"Verify the path is correct and exists. Use an absolute path or ensure the relative path is correct from your current working directory. For example: `deadcode --path .` to scan the current directory.","cause":"The path provided to the `--path` argument does not exist or is inaccessible.","error":"Error: Invalid value for '--path': Path '/non_existent_path' does not exist."},{"fix":"If you don't need custom configuration, remove any `--config` flag or ensure the `deadcode.toml` file exists in the expected location. To generate a default config, you might need to run `deadcode config init` if such a command exists (check documentation).","cause":"The `deadcode` command expects a `deadcode.toml` configuration file in the working directory or specified path if you're trying to use custom configuration.","error":"Error reading configuration file: No such file or directory: 'deadcode.toml'"}]}