Skip to main content

Detect and install Python dependencies from import statements

Project description

Pipwatch

PyPI version CI License: MIT Python 3.10+ Code style: black

Detect import statements in Python files and install the corresponding PyPI packages.

Pipwatch parses your Python files for imports, filters out standard library modules, maps import names to PyPI package names where they differ (e.g. PIL to pillow, cv2 to opencv-python), and installs whatever is missing.

Features

Import Detection

  • Parses import statements using the AST module and filters out standard library modules
  • Also detects # pip install comments and version specifiers in your code
  • Maps import names to correct PyPI package names (e.g. sklearn -> scikit-learn)
  • Skips packages that are already installed

Mapping Registry

  • Package mappings hosted on GitHub and fetched at runtime
  • Local cache with 24-hour TTL so it doesn't hit the network on every run
  • Falls back to bundled mappings.json when offline
  • New mappings can be added via pull request

CLI Flags

  • --dry-run - Preview what would be installed without installing anything
  • --generate - Generate a requirements.txt file from detected dependencies
  • --verbose - Enable detailed debug logging
  • --yes - Auto-confirm installations (useful for CI)
  • --output - Specify custom output path for generated requirements files
  • --update-mappings - Fetch latest mappings from GitHub
  • --clear-cache - Clear cached mappings
  • --show-mappings - Show mapping statistics

Installation

pip install pipwatch

For development:

pip install pipwatch[dev]

Usage

Basic Usage

Analyze and install dependencies:

pipwatch script.py

Dry Run (Preview Only)

See what would be installed without installing:

pipwatch script.py --dry-run

Example output:

INFO: Analyzing script.py...
INFO: Found the following dependencies from import statements:
INFO:   • bs4 → beautifulsoup4
INFO:   • numpy
INFO:   • pandas

[DRY RUN] Would install the following packages:
  • beautifulsoup4
  • numpy
  • pandas

Generate Requirements File

Create a requirements.txt from your code:

pipwatch script.py --generate

Or with a custom output path:

pipwatch script.py --generate --output my-requirements.txt

Auto-Install (No Prompts)

For automation and CI:

pipwatch script.py --yes

Verbose Mode

Get detailed debug information:

pipwatch script.py --verbose

Manage Mappings

Update mappings from GitHub:

pipwatch --update-mappings

Show mapping statistics:

pipwatch --show-mappings

Clear cached mappings:

pipwatch --clear-cache

Package Name Mappings

Pipwatch includes 50+ mappings for cases where the import name differs from the PyPI package name:

Import Name Package Name
PIL pillow
cv2 opencv-python
sklearn scikit-learn
bs4 beautifulsoup4
yaml pyyaml
Crypto pycryptodome
jwt PyJWT
And more...

How It Works

  1. Parse - Parses your Python file using the AST module
  2. Filter - Removes standard library imports
  3. Analyze - Scans for # pip install comments and version specifiers
  4. Map - Maps import names to PyPI package names
  5. Check - Checks which packages are already installed
  6. Install/Generate - Installs missing packages or generates requirements.txt

Mapping Resolution

The mapping system loads from three sources, trying each in order:

┌─────────────────┐
│  mappings.json  │ (GitHub)
│   (Remote)      │
└────────┬────────┘
         │ Fetch (once per day)
         ▼
┌─────────────────┐
│  Local Cache    │ (~/.pipwatch/)
│  (24hr TTL)     │
└────────┬────────┘
         │ Fallback
         ▼
┌─────────────────┐
│  Bundled File   │ (Offline support)
│  (Built-in)     │
└─────────────────┘

Contributing Mappings

Found a missing mapping? See docs/CONTRIBUTING_MAPPINGS.md for how to add one.

Development

Setup

git clone https://github.com/Felixdiamond/pipwatch.git
cd pipwatch
pip install -e ".[dev]"

Run Tests

pytest tests/

With coverage:

pytest tests/ --cov=src/pipwatch --cov-report=term-missing

Code Formatting

black src/ tests/
isort src/ tests/
ruff check src/ tests/ --fix

Contributing

See CONTRIBUTING.md for guidelines.

License

MIT License


Created by Felix Dawodu

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pipwatch-0.0.1.tar.gz (21.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pipwatch-0.0.1-py3-none-any.whl (11.9 kB view details)

Uploaded Python 3

File details

Details for the file pipwatch-0.0.1.tar.gz.

File metadata

  • Download URL: pipwatch-0.0.1.tar.gz
  • Upload date:
  • Size: 21.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pipwatch-0.0.1.tar.gz
Algorithm Hash digest
SHA256 50ec4e61944b742f23b4658edce751bcfb6bc5d468a52d144935c074eaaf2c3f
MD5 f185f6cfe87099f2183bfb050b6fd3d1
BLAKE2b-256 62db3eb75e861e7217e24b7afc00615cd7be8ea5fd91ad2d38bff9a53c66888e

See more details on using hashes here.

Provenance

The following attestation bundles were made for pipwatch-0.0.1.tar.gz:

Publisher: release.yml on Felixdiamond/pipwatch

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pipwatch-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: pipwatch-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 11.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pipwatch-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 118a3d5806a4bfb2173ff18c2a0d774133b35b2c49c44debfa0bac4c33f6de4e
MD5 0af71d1ed870c09e5b3c6d5968b7d4e8
BLAKE2b-256 e61b0b194e6536c932d846cb2691b3f07feaef76f5a1164baa9c897d699c7366

See more details on using hashes here.

Provenance

The following attestation bundles were made for pipwatch-0.0.1-py3-none-any.whl:

Publisher: release.yml on Felixdiamond/pipwatch

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page