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 a single file:

pipwatch script.py

Analyze all Python files in the current directory & subdirectories:

pipwatch .

Analyze all Python files in a specific folder:

pipwatch myfolder/

Dry Run (Preview Only)

See what would be installed without installing:

pipwatch script.py --dry-run

Or for an entire directory:

pipwatch . --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

Update Mappings

Use the helper script to add or discover import-to-package mappings in src/pipwatch/mappings.json.

Add a single mapping (validated against PyPI):

python scripts/populate_mappings.py add PIL pillow --category image-processing

Preview without writing:

python scripts/populate_mappings.py add PIL pillow --category image-processing --dry-run

Discover mappings from your installed packages and preview:

python scripts/populate_mappings.py discover

Apply discovered mappings:

python scripts/populate_mappings.py discover --apply

Preview discovered mappings without writing:

python scripts/populate_mappings.py discover --apply --dry-run

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.2.tar.gz (24.1 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.2-py3-none-any.whl (12.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pipwatch-0.0.2.tar.gz
  • Upload date:
  • Size: 24.1 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.2.tar.gz
Algorithm Hash digest
SHA256 de7da9a19d6c90239ad891287c733ae5041b4103bb0fb0cb806aa3017f048467
MD5 45b2ecb6f085caea1a0d6346b0199940
BLAKE2b-256 5ca2ca093548f118e193f0783063c002318b964db341fbd136d112d8ab2053f4

See more details on using hashes here.

Provenance

The following attestation bundles were made for pipwatch-0.0.2.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.2-py3-none-any.whl.

File metadata

  • Download URL: pipwatch-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 12.5 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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 cac34cc482ba6e8cb3b365052906ead210b72fe9f1d39061bf6c0e0780772139
MD5 47214f0e6795002b2c2fa660a73cd813
BLAKE2b-256 633d8b0a4149b01fc1b60cfdbe7edaaed0b49d39eef2296f0cfd5e2bab18333f

See more details on using hashes here.

Provenance

The following attestation bundles were made for pipwatch-0.0.2-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