Detect and install Python dependencies from import statements
Project description
Pipwatch
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 installcomments 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.jsonwhen offline - New mappings can be added via pull request
CLI Flags
--dry-run- Preview what would be installed without installing anything--generate- Generate arequirements.txtfile 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
- Parse - Parses your Python file using the AST module
- Filter - Removes standard library imports
- Analyze - Scans for
# pip installcomments and version specifiers - Map - Maps import names to PyPI package names
- Check - Checks which packages are already installed
- 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
Created by Felix Dawodu
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
de7da9a19d6c90239ad891287c733ae5041b4103bb0fb0cb806aa3017f048467
|
|
| MD5 |
45b2ecb6f085caea1a0d6346b0199940
|
|
| BLAKE2b-256 |
5ca2ca093548f118e193f0783063c002318b964db341fbd136d112d8ab2053f4
|
Provenance
The following attestation bundles were made for pipwatch-0.0.2.tar.gz:
Publisher:
release.yml on Felixdiamond/pipwatch
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pipwatch-0.0.2.tar.gz -
Subject digest:
de7da9a19d6c90239ad891287c733ae5041b4103bb0fb0cb806aa3017f048467 - Sigstore transparency entry: 944038111
- Sigstore integration time:
-
Permalink:
Felixdiamond/pipwatch@a412ef67e7c7d6fdd17d29a8ad8128fa1f2ba685 -
Branch / Tag:
refs/tags/v0.0.2 - Owner: https://github.com/Felixdiamond
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@a412ef67e7c7d6fdd17d29a8ad8128fa1f2ba685 -
Trigger Event:
push
-
Statement type:
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cac34cc482ba6e8cb3b365052906ead210b72fe9f1d39061bf6c0e0780772139
|
|
| MD5 |
47214f0e6795002b2c2fa660a73cd813
|
|
| BLAKE2b-256 |
633d8b0a4149b01fc1b60cfdbe7edaaed0b49d39eef2296f0cfd5e2bab18333f
|
Provenance
The following attestation bundles were made for pipwatch-0.0.2-py3-none-any.whl:
Publisher:
release.yml on Felixdiamond/pipwatch
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pipwatch-0.0.2-py3-none-any.whl -
Subject digest:
cac34cc482ba6e8cb3b365052906ead210b72fe9f1d39061bf6c0e0780772139 - Sigstore transparency entry: 944038116
- Sigstore integration time:
-
Permalink:
Felixdiamond/pipwatch@a412ef67e7c7d6fdd17d29a8ad8128fa1f2ba685 -
Branch / Tag:
refs/tags/v0.0.2 - Owner: https://github.com/Felixdiamond
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@a412ef67e7c7d6fdd17d29a8ad8128fa1f2ba685 -
Trigger Event:
push
-
Statement type: