Skip to main content

A smart CLI tool that scans Python projects for imports and auto-generates requirements.txt with latest PyPI versions

Project description

PyReqScan

CI Python Version License: MIT

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 .venv on 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


Download files

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

Source Distribution

pyreqscan-1.0.0.tar.gz (27.1 kB view details)

Uploaded Source

Built Distribution

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

pyreqscan-1.0.0-py3-none-any.whl (21.6 kB view details)

Uploaded Python 3

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

Hashes for pyreqscan-1.0.0.tar.gz
Algorithm Hash digest
SHA256 ceeafb8a0035bde37dea66c395a488b9d024fb97432afae3238a608d944e899e
MD5 bbc90e5d5c4b462678b7f942fdeea28b
BLAKE2b-256 f3974cffd7758b7fed2621243d49898672d178f1d99c62a0fd9645a41909e453

See more details on using hashes here.

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

Hashes for pyreqscan-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ac455c47abe05b2dd496e11a808caf6f77c6b733c74a67a4f41f3f356f97ddfb
MD5 411380d0b62775e262197c3b791c19ec
BLAKE2b-256 a9e6d030f421e8c0b205c50b596704e48b5545e64863973e37954d817d174286

See more details on using hashes here.

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