Skip to content

Python Requirements ParserHigh-performance requirements.txt parser and editor

Parse, edit, and manage Python dependencies with ease

Python Requirements Parser

Quick Example

go
package main

import (
    "fmt"
    "log"
    
    "github.com/scagogogo/python-requirements-parser/pkg/parser"
    "github.com/scagogogo/python-requirements-parser/pkg/editor"
)

func main() {
    // Parse requirements.txt
    p := parser.New()
    reqs, err := p.ParseFile("requirements.txt")
    if err != nil {
        log.Fatal(err)
    }
    
    // Edit with position-aware editor (minimal diff)
    editor := editor.NewPositionAwareEditor()
    doc, err := editor.ParseRequirementsFile(content)
    if err != nil {
        log.Fatal(err)
    }
    
    // Update package versions
    updates := map[string]string{
        "flask":   "==2.0.1",
        "django":  ">=3.2.13",
        "requests": ">=2.28.0",
    }
    
    err = editor.BatchUpdateVersions(doc, updates)
    if err != nil {
        log.Fatal(err)
    }
    
    // Serialize with minimal changes
    result := editor.SerializeToString(doc)
    fmt.Println(result)
}

Key Features

🚀 Three Powerful Editors

  • VersionEditor - Basic text-based editing
  • VersionEditorV2 - Parser-based reconstruction editing
  • PositionAwareEditor - Position-based minimal diff editing ⭐

📊 Performance Benchmarks

OperationTimeMemoryAllocations
Parse 100 packages357 µs480 KB4301 allocs
Single update67.67 ns8 B1 alloc
Batch update (10 packages)374.1 ns0 B0 allocs
Serialize 100 packages4.3 µs8.2 KB102 allocs

🎯 Minimal Diff Editing

The PositionAwareEditor achieves 50% fewer changes compared to traditional editors:

  • Real-world test: 68-line requirements.txt file
  • PositionAwareEditor: 5.9% change rate (4/68 lines)
  • Traditional editor: 11.8% change rate (8/68 lines)

Perfect preservation of:

  • ✅ Comments and formatting
  • ✅ VCS dependencies (git+https://...)
  • ✅ URL dependencies (https://...)
  • ✅ File references (-r requirements-dev.txt)
  • ✅ Environment markers (; python_version >= "3.7")
  • ✅ Global options (--index-url https://...)

Supported Formats

Full support for all pip-compatible formats:

txt
# Basic dependencies
flask==2.0.1
django>=3.2.0,<4.0.0
requests~=2.25.0

# Dependencies with extras
django[rest,auth]>=3.2.0
uvicorn[standard]>=0.15.0

# Environment markers
pywin32>=1.0; platform_system == "Windows"
dataclasses>=0.6; python_version < "3.7"

# VCS dependencies
git+https://github.com/user/project.git#egg=project
-e git+https://github.com/dev/project.git@develop#egg=project

# URL dependencies
https://example.com/package.whl
http://mirrors.aliyun.com/pypi/web/package-1.0.0.tar.gz

# File references
-r requirements-dev.txt
-c constraints.txt

# Global options
--index-url https://pypi.example.com
--extra-index-url https://private.pypi.com
--trusted-host pypi.example.com

# Hash verification
flask==2.0.1 --hash=sha256:abcdef1234567890

Getting Started

  1. Quick Start - Get up and running in minutes
  2. API Reference - Complete API documentation
  3. Examples - Progressive examples and tutorials
  4. Performance Guide - Production best practices

Community

License

Released under the MIT License.

Released under the MIT License