8000 GitHub - abevol/pip-sweep · GitHub
[go: up one dir, main page]

Skip to content
abevolPublic

About

No description, website, or topics provided.

Resources

Stars

1 star

Watchers

0 watching

Forks

Repository files navigation

[English] | 中文

pip-sweep

pip-sweep is a command-line tool to clean Python package dependencies using the Mark-Sweep garbage collection algorithm.

When you uninstall large frameworks like django, running pip uninstall only removes the main package itself, leaving a large number of downstream dependencies installed in your environment, creating "dependency garbage".

pip-sweep intelligently analyzes the dependency graph of the current environment to safely identify and completely uninstall those orphaned packages that are only required by the target package and not used by any other surviving packages.

⚠️ Critical: Execution Environment & pipx/uvx Incompatibility

pip-sweep operates strictly on the active Python environment in which it is running.

  • Virtual Environments (venv/conda): To clean packages inside a virtual environment, you must activate the environment, install pip-sweep inside it, and run it.
  • Global Environment: To clean globally installed packages, pip-sweep must be run by the global Python interpreter.
  • pipx run / uvx Incompatibility: Using pipx run pip-sweep or uvx pip-sweep WILL NOT WORK. These tools run the cleaner inside an isolated, empty, temporary virtual environment containing only pip-sweep itself. Consequently, they cannot see or clean your active virtual environment or global packages. Do not use pipx run or uvx to execute this tool.

✨ Features

  • 🧩 Mark-Sweep Garbage Collection: Accurate dependency propagation algorithm to prevent accidental uninstallation.
  • 🛡️ System Protection: Protects pip, setuptools, wheel, and other baseline infrastructure packages by default.
  • 🔍 Dry-Run Mode: Supports -d / --dry-run to preview the uninstallation plan safely.
  • 💎 Multi-package Support: Clean multiple target packages at once.
  • 📌 Specify Packages to Keep: Use -e / --exclude to specify packages (and their own dependencies) that should be kept during uninstallation.

🚀 Installation & Usage

To clean packages, you must run pip-sweep inside the target Python environment.

1. Activate your target virtual environment (if using one)

Choose the command corresponding to your operating system and shell:

  • macOS / Linux (bash/zsh):
    source .venv/bin/activate
  • Windows (PowerShell):
    .venv\Scripts\Activate.ps1
  • Windows (cmd.exe):
    .venv\Scripts\activate.bat
  • Conda Environment:
    conda activate myenv

(Note: If you want to clean system-wide global packages, skip this activation step).

2. Install into the active environment

pip install pip-sweep
# Or using uv
uv pip install pip-sweep

3. Run the cleaner

pip-sweep <package-name>

3.1 Keeping specific packages while uninstalling

Use -e / --exclude to keep packages that would otherwise be uninstalled together with the target

# Keep rfc3986 and requests-toolbelt (and anything they depend on)
pip-sweep twine -e rfc3986 requests-toolbelt

# Equivalent: repeat the flag, or use commas
pip-sweep twine -e rfc3986 -e requests-toolbelt
pip-sweep twine -e rfc3986,requests-toolbelt

The kept packages themselves and their dependency subtree survive; the target package itself cannot be excluded.

Tip

Because -e accepts a space-separated list, when using spaces as the separator, put it after the target packages, otherwise argparse will treat the following names as part of the keep list.

📖 Command-line Usage

usage: pip-sweep [-h] [-d] [-y] [-v] [-e PACKAGE [PACKAGE ...]] packages [packages ...]

pip-sweep: A Python dependency cleaner using Mark-Sweep garbage collection algorithm.

positional arguments:
  packages              One or more target Python packages to clean

options:
  -h, --help            Show this help message and exit
  -d, --dry-run         Analyze and print the clean plan only, without actual uninstallation
  -y, --yes             Automatically confirm the uninstallation plan, skipping confirmation prompts
  -v, --verbose         Verbose output, showing kept packages and their specific reasons
  -e, --exclude PACKAGE [PACKAGE ...]
                        Package names to keep (space-separated, or repeat -e); these
                        packages and their dependencies will not be uninstalled
  --version             Show program's version number and exit

🔧 Local Development

To debug or contribute locally:

# Clone the repository
git clone https://github.com/abevol/pip-sweep.git
cd pip-sweep

# Create venv and install in editable mode
uv pip install -e .

📝 License

MIT License © 2026 pip-sweep developers

About

No description, website, or topics provided.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages

0