A smart CLI tool that scans Python projects for imports and auto-generates requirements.txt with latest PyPI versions
Project description
PyReqScan
A smart CLI tool that scans Python projects for import statements and auto-generates requirements.txt with the latest PyPI versions.
โจ Features
- ๐ AST-Based Scanning โ Parses Python files using the Abstract Syntax Tree for accurate import detection (no regex hacks)
- ๐ง Smart Stdlib Detection โ Dynamically identifies standard library modules using
sys.stdlib_module_names(Python 3.10+) with fallbacks - ๐ PyPI Version Resolution โ Fetches the latest stable version for each package from PyPI's JSON API
- โก Concurrent Resolution โ Resolves multiple package versions in parallel using ThreadPoolExecutor
- ๐ฆ Import โ PyPI Mapping โ Handles packages where import name โ PyPI name (
cv2โopencv-python,PILโPillow,yamlโPyYAML) - ๐ซ Smart Exclusions โ Automatically skips
venv/,node_modules/,__pycache__/,.git/, etc. - ๐ ๏ธ Environment Setup โ One-command virtual environment creation with auto-installation (
--init-env --install). - ๐ VS Code Integration โ Automatically configures VS Code (
.vscode/settings.json) to activate your.venvon new terminals. - ๐จ Colorized Output โ Rich terminal output with progress indicators
- ๐ฅ๏ธ Cross-Platform โ Works on Windows, macOS, and Linux
๐ฆ Installation
Using pip
pip install pyreqscan
Using pipx (recommended for CLI tools)
pipx install pyreqscan
From source
git clone https://github.com/crrrowz/pyreqscan.git
cd pyreqscan
pip install -e .
๐ Usage
Basic Usage
# Scan current directory and generate requirements.txt
pyreqscan
# Scan a specific project directory
pyreqscan ./my-project
# Preview without writing (dry run)
pyreqscan --dry-run
# Generate requirements.txt AND automatically pip install them
pyreqscan . --install
# Create a virtual environment (.venv) and configure VS Code auto-activation
pyreqscan . --init-env
# Full Setup: Create venv, configure VS Code, generate requirements, and install them
pyreqscan . --init-env --install
Auto-Activation Wrapper Scripts
If you want a one-click setup that also activates the environment in your current terminal session, you can use the included wrapper scripts from the scripts/ directory:
# Windows (PowerShell)
.\scripts\setup-env.ps1
# macOS / Linux (Must use 'source' to modify current shell)
source scripts/setup-env.sh
Version Pinning Styles
# Compatible release (default): ~=2.31.0
pyreqscan --pin compatible
# Exact version: ==2.31.0
pyreqscan --pin exact
# Minimum version: >=2.31.0
pyreqscan --pin minimum
# No version pinning
pyreqscan --pin none
Offline Mode
# Skip PyPI version lookups (just list package names)
pyreqscan --no-version
Other Options
# Custom output file
pyreqscan -o deps.txt
# Verbose output (show discovered packages & progress)
pyreqscan -v
# Don't include header comments
pyreqscan --no-header
# Don't backup existing requirements.txt
pyreqscan --no-backup
# Disable colors
pyreqscan --no-color
๐ Example Output
Running pyreqscan on a Flask project:
๐ฆ PyReqScan โ Scanning for imports...
Scanned 12 Python files
Found 8 third-party imports (15 stdlib skipped)
๐ Resolving latest versions from PyPI...
[1/8] โ Flask โ 3.0.0
[2/8] โ requests โ 2.31.0
[3/8] โ SQLAlchemy โ 2.0.23
[4/8] โ celery โ 5.3.6
[5/8] โ redis โ 5.0.1
[6/8] โ gunicorn โ 21.2.0
[7/8] โ python-dotenv โ 1.0.0
[8/8] โ PyYAML โ 6.0.1
Resolved 8/8 versions
โ
Generated requirements.txt with 8 dependencies
Generated requirements.txt:
# Auto-generated by pyreqscan on 2024-12-24 12:00:00 UTC
# Scanned 12 Python files
# Found 8 third-party dependencies
#
celery~=5.3.6
Flask~=3.0.0
gunicorn~=21.2.0
python-dotenv~=1.0.0
PyYAML~=6.0.1
redis~=5.0.1
requests~=2.31.0
SQLAlchemy~=2.0.23
โ๏ธ CLI Reference
usage: pyreqscan [OPTIONS] [DIRECTORY]
Scan Python projects for imports and auto-generate requirements.txt.
positional arguments:
directory Directory to scan (default: current directory)
options:
-h, --help Show this help message
-V, --version Show version number
-o, --output FILE Output file path (default: requirements.txt)
--pin STYLE Version pinning: exact, compatible, minimum, none
--no-version Skip PyPI version resolution (offline mode)
--no-header Don't include header comments
--dry-run Preview output without writing to file
--no-backup Don't backup existing requirements.txt
--include-stdlib Include standard library modules in output
--no-color Disable colorized output
-v, --verbose Show detailed scanning information
--install Auto-install packages after generating
--init-env Create a .venv virtual environment
--env-path PATH Virtual environment directory name (default: .venv)
๐ง How It Works
โโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโ
โ 1. SCAN โ โโโถ โ 2. RESOLVE โ โโโถ โ 3. GENERATE โ
โ โ โ โ โ โ
โ Walk .py files โ โ Query PyPI API โ โ Format output โ
โ Parse with AST โ โ Get latest โ โ Pin versions โ
โ Extract imports โ โ stable versions โ โ Write file โ
โ Filter stdlib โ โ (concurrent) โ โ Backup old โ
โ Detect local โ โ โ โ โ
โโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโ
Stdlib Detection Strategy
| Priority | Method | Python Version |
|---|---|---|
| 1 | sys.stdlib_module_names |
3.10+ |
| 2 | stdlib-list package |
3.8 - 3.9 |
| 3 | importlib.metadata heuristic |
All |
| 4 | Minimal built-in fallback | All |
Import Name Resolution
Many PyPI packages use different names for installation vs import:
| Import Name | PyPI Package |
|---|---|
cv2 |
opencv-python |
PIL |
Pillow |
sklearn |
scikit-learn |
bs4 |
beautifulsoup4 |
yaml |
PyYAML |
dateutil |
python-dateutil |
dotenv |
python-dotenv |
attr |
attrs |
PyReqScan ships with 100+ known mappings and also uses importlib.metadata.packages_distributions() (Python 3.11+) for dynamic runtime resolution.
๐ Python API
from pyreqscan import scan_directory, resolve_package_version, generate_requirements
from pyreqscan.resolver import resolve_multiple
# Scan a project
result = scan_directory("./my-project")
# Get third-party imports
for name, info in result.third_party.items():
print(f"{info.pypi_name}: imported in {len(info.source_files)} files")
# Resolve versions
pypi_names = [info.pypi_name for info in result.third_party.values()]
versions = resolve_multiple(pypi_names)
# Generate requirements.txt content
content = generate_requirements(result, versions, pin_style="exact")
print(content)
๐ Default Scan Exclusions
| Category | Patterns |
|---|---|
| Version Control | .git, .hg, .svn |
| Python | __pycache__, .pytest_cache, .mypy_cache, .eggs |
| Virtual Env | venv, .venv, env, ENV, site-packages |
| Node.js | node_modules, .npm, .yarn |
| Build | dist, build, out, target |
| IDE | .idea, .vscode, .vs |
๐ค Contributing
Contributions are welcome! Please see CONTRIBUTING.md for guidelines.
๐ License
This project is licensed under the MIT License - see the LICENSE file for details.
๐ Acknowledgments
Inspired by pipreqs and the need for a modern, accurate, AST-based requirements generator.
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 pyreqscan-1.0.0.tar.gz.
File metadata
- Download URL: pyreqscan-1.0.0.tar.gz
- Upload date:
- Size: 27.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ceeafb8a0035bde37dea66c395a488b9d024fb97432afae3238a608d944e899e
|
|
| MD5 |
bbc90e5d5c4b462678b7f942fdeea28b
|
|
| BLAKE2b-256 |
f3974cffd7758b7fed2621243d49898672d178f1d99c62a0fd9645a41909e453
|
File details
Details for the file pyreqscan-1.0.0-py3-none-any.whl.
File metadata
- Download URL: pyreqscan-1.0.0-py3-none-any.whl
- Upload date:
- Size: 21.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ac455c47abe05b2dd496e11a808caf6f77c6b733c74a67a4f41f3f356f97ddfb
|
|
| MD5 |
411380d0b62775e262197c3b791c19ec
|
|
| BLAKE2b-256 |
a9e6d030f421e8c0b205c50b596704e48b5545e64863973e37954d817d174286
|