Generate Python Requirements.txt
Actions🚀 Available on GitHub Actions Marketplace
Note: This project is a fork and enhancement of ryan-rozario/pipreqs-action with significant improvements and additional features.
Automatically generates requirements.txt files for Python projects using pipreqs with enhanced duplicate handling, GitHub Actions integration, and comprehensive testing.
- 🔄 Automatic Requirements Generation: Uses pipreqs to analyze Python imports
- 🔍 Recursive Scanning: Optional recursive directory scanning
- ⚡ Duplicate Handling: Intelligent handling of duplicate packages with different versions
⚠️ GitHub Warnings: Automatic warnings in GitHub Actions logs for conflicts- 🧪 Comprehensive Testing: 100% test coverage with robust error handling
- 🐳 Docker Support: Containerized execution for consistent environments
- 📊 Detailed Logging: Enhanced logging for debugging and monitoring
Add this action to your workflow to automatically generate or update your requirements.txt:
name: Update Requirements
on: [push, pull_request]
jobs:
requirements:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v5
- name: Generate Requirements
uses: jfheinrich-eu/pipreqs-action@v4.4.2 # Use latest stable version
with:
PROJECT_PATH: "."
REQUIREMENT_PATH: "requirements.txt"
RECURSIVE: "true"
- name: Commit Changes
uses: stefanzweifel/git-auto-commit-action@v5
with:
commit_message: 'chore: update requirements.txt [skip ci]'
file_pattern: requirements.txt| Input | Description | Required | Default |
|---|---|---|---|
PROJECT_PATH |
Path to the project folder containing Python files | ✅ | /github/workspace |
REQUIREMENT_PATH |
Path where requirements.txt should be saved (including filename) |
✅ | /github/workspace/requirements.txt |
RECURSIVE |
Whether to scan directories recursively ('true' or 'false') |
✅ | 'true' |
- name: Generate Requirements for Specific Directory
uses: jfheinrich-eu/pipreqs-action@v4.1.0 # Or pin to SHA: @a1b2c3d4...
with:
PROJECT_PATH: src/my_package
REQUIREMENT_PATH: requirements/base.txt
RECURSIVE: 'false'- name: Generate Requirements for API
uses: jfheinrich-eu/pipreqs-action@v4.1.0 # Or pin to SHA: @a1b2c3d4...
with:
PROJECT_PATH: api
REQUIREMENT_PATH: api/requirements.txt
RECURSIVE: 'true'
- name: Generate Requirements for Web App
uses: jfheinrich-eu/pipreqs-action@v4.1.0 # Or pin to SHA: @a1b2c3d4...
with:
PROJECT_PATH: webapp
REQUIREMENT_PATH: webapp/requirements.txt
RECURSIVE: 'true'For maximum security in production environments, pin to specific commit SHA:
- name: Generate Requirements (SHA Pinned)
uses: jfheinrich-eu/pipreqs-action@a1b2c3d4e5f6789012345678901234567890abcd
with:
PROJECT_PATH: src
REQUIREMENT_PATH: requirements.txt
RECURSIVE: 'true'Why SHA pinning? Commit SHAs are immutable and prevent supply chain attacks via tag manipulation.
This fork includes several enhancements over the original ryan-rozario/pipreqs-action:
- Smart Version Resolution: Automatically selects the highest version when duplicate packages are found
- Conflict Warnings: Displays GitHub Actions warnings when version conflicts are detected
- Clean Output: Eliminates duplicate entries in
requirements.txt
- 98% Test Coverage: Comprehensive unit and integration tests
- Type Safety: Full type annotations and mypy compliance
- Code Quality: Pylint, Black, and isort integration
- Pre-commit Hooks: Automated code quality checks
- Poetry Support: Modern Python dependency management
- Docker Integration: Containerized execution environment
- CI/CD Pipeline: Automated testing and release workflows
- Documentation: Comprehensive README and inline documentation
- Python 3.12+
- Poetry
- Docker (optional)
# Clone the repository
git clone https://github.com/jfheinrich-eu/pipreqs-action.git
cd pipreqs-action
# Install dependencies
poetry install --all-groups
# Run tests
poetry poe test
# Run linting
poetry poe lint
# Format code
poetry poe formatThe repository includes a robust script for managing Poetry virtual environments, which helps maintain a consistent development environment.
# Run with Poetry Exec
poetry poe reset_venv
# Or directly from scripts folder
./scripts/reset-poetry-venvAvailable options:
Options:
-h, --help Show this help message
-r, --reset Completely reset the environment (includes cache and config)
-u, --update Update the existing environment (default)
-n, --new Create a new environment
-p, --python VERSION Specify Python version (e.g., 3.12)
-d, --delete-venv Delete the virtual environment only
-c, --clear-cache Clear Poetry cache
-g, --clear-config Clear Poetry configuration
-i, --install-poetry Reinstall Poetry
-v, --verbose Enable verbose output
Examples:
./scripts/reset-poetry-venv --reset # Complete reset
./scripts/reset-poetry-venv --update # Update dependencies
./scripts/reset-poetry-venv --new --python 3.12 # Create with Python 3.12
When run without parameters, the script enters interactive mode with a menu of options. The script automatically configures VS Code to use the Poetry virtual environment.
# Run all tests with coverage
poetry poe test
# Run specific test file
pytest tests/test_main.py -v
# Generate coverage report
pytest --cov --cov-report=htmlmy-project/
├── src/
│ ├── main.py # imports: requests, pandas
│ └── utils.py # imports: numpy, matplotlib
├── requirements.txt # Generated: requests, pandas, numpy, matplotlib
└── .github/
└── workflows/
└── requirements.yml
monorepo/
├── api/
│ ├── app.py # imports: fastapi, sqlalchemy
│ └── requirements.txt # Generated: fastapi, sqlalchemy
├── worker/
│ ├── tasks.py # imports: celery, redis
│ └── requirements.txt # Generated: celery, redis
└── .github/workflows/requirements.yml
This action is available on the GitHub Actions Marketplace. It falls under the following categories:
- Automatically analyzes Python projects for dependencies
- Generates clean, deduplicated requirements.txt files
- Handles version conflicts intelligently
- Integrates seamlessly with GitHub Actions workflows
- Provides detailed logging and error reporting
- Supports various project structures and layouts
- Specialized for Python ecosystem
- Works with all major Python package managers
- Supports both standard library and third-party packages
- Version Pinning: Always use a specific version tag (e.g.,
@v4.4.2) for production workflows - Security: Consider pinning to a specific commit SHA for maximum security
- Testing: Test the action in a feature branch before deploying to main workflows
- Monitoring: Enable GitHub Actions notifications to stay informed about workflow runs
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'feat: add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- Original Project: ryan-rozario/pipreqs-action by @ryan-rozario
- Contributors: @afonsoVale for contributions to the original project
- pipreqs: bndr/pipreqs for the core functionality
- Test Coverage: 98%
- Python Version: 3.12+
- Dependencies: Minimal and well-maintained
- Docker Image Size: Optimized for CI/CD environments
Generate Python Requirements.txt is not certified by GitHub. It is provided by a third-party and is governed by separate terms of service, privacy policy, and support documentation.