FawltyDeps

0.20.0 · active · verified Thu Apr 16

FawltyDeps is a command-line tool designed to find undeclared and unused third-party dependencies in Python projects. It analyzes Python code for import statements and compares them against declared dependencies in `pyproject.toml`, `requirements.txt`, `environment.yml`, and `pixi.toml` files. The current version is 0.20.0, with a release cadence of roughly once a month for minor versions.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to set up a minimal project and then run `fawltydeps` via a Python `subprocess` call to identify undeclared and unused dependencies. It creates dummy `src/app.py` and `requirements.txt` files, runs the tool, and prints its output, then cleans up.

import os
import subprocess
import shutil

# --- Setup a dummy project for demonstration ---
project_dir = "my_fawltydeps_example_project"
os.makedirs(os.path.join(project_dir, "src"), exist_ok=True)

# Create a Python file with imports
with open(os.path.join(project_dir, "src", "app.py"), "w") as f:
    f.write("import requests\nimport pandas\nfrom os import path\n")

# Create a requirements file with some declared dependencies
with open(os.path.join(project_dir, "requirements.txt"), "w") as f:
    f.write("requests==2.31.0\n")

# --- Run FawltyDeps via subprocess ---
print(f"\nRunning fawltydeps in '{project_dir}' to find mismatches...\n")
try:
    # Execute the fawltydeps command line tool
    result = subprocess.run(
        ["fawltydeps", "--verbose", "--code", "src", "--deps", "requirements.txt"],
        cwd=project_dir,
        capture_output=True,
        text=True,
        check=False # Set to True if you expect a non-zero exit for issues
    )
    print("FawltyDeps Output:\n")
    print(result.stdout)
    if result.stderr:
        print("FawltyDeps Errors:\n")
        print(result.stderr)
except FileNotFoundError:
    print("Error: 'fawltydeps' command not found. Please install it with 'pip install fawltydeps'.")
except Exception as e:
    print(f"An unexpected error occurred: {e}")
finally:
    # --- Clean up the dummy project ---
    if os.path.exists(project_dir):
        shutil.rmtree(project_dir)
        print(f"\nCleaned up example project directory: '{project_dir}'")

view raw JSON →