or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

exceptions.mdindex.mdlockfile.mdpipfile.mdrequirement.md
tile.json

tessl/pypi-requirementslib

A tool for converting between pip-style and pipfile requirements.

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
pypipkg:pypi/requirementslib@3.0.x

To install, run

npx @tessl/cli install tessl/pypi-requirementslib@3.0.0

index.mddocs/

RequirementsLib

A comprehensive Python library that provides a unified interface for managing and converting between different Python package requirement formats, specifically Pipfile and requirements.txt formats. RequirementsLib offers robust functionality for parsing, manipulating, and converting package requirements across different dependency management ecosystems, with particular focus on Pipenv and pip compatibility.

Package Information

  • Package Name: requirementslib
  • Language: Python
  • Installation: pip install requirementslib

Core Imports

from requirementslib import Requirement, Pipfile, Lockfile
from requirementslib.utils import is_vcs, is_editable, VCS_LIST

Basic Usage

from requirementslib import Requirement, Pipfile, Lockfile

# Parse a requirement from a requirements.txt line
req = Requirement.from_line("requests>=2.25.0")
print(req.name)           # "requests"
print(req.specifiers)     # ">=2.25.0"

# Convert to different formats
print(req.as_line())      # "requests>=2.25.0"
pipfile_entry = req.as_pipfile()  # {"requests": ">=2.25.0"}

# Load and work with Pipfiles
pipfile = Pipfile.load("./Pipfile")
for req in pipfile.requirements:
    print(f"{req.name}: {req.specifiers}")

# Load and work with lockfiles
lockfile = Lockfile.load("./Pipfile.lock")
for req in lockfile.get_requirements():
    print(f"{req.name}: {req.specifiers}")

# Convert lockfile requirements to requirements.txt format
requirements_lines = lockfile.as_requirements("default")
for line in requirements_lines:
    print(line)

Architecture

RequirementsLib is built around three main classes that handle different aspects of Python dependency management:

  • Requirement: Core class for individual package requirements with extensive parsing and conversion capabilities
  • Pipfile: Interface for reading and manipulating Pipfile format used by Pipenv
  • Lockfile: Interface for reading and manipulating Pipfile.lock format for reproducible builds

The library provides seamless conversion between formats while preserving all metadata including version constraints, environment markers, VCS requirements, editable installs, and package hashes.

Capabilities

Requirement Processing

Core functionality for parsing, manipulating, and converting individual package requirements. Handles named packages, VCS requirements, file/URL requirements, editable installs, environment markers, and package hashes.

class Requirement:
    @classmethod
    def from_line(cls, line, parse_setup_info=True): ...
    @classmethod
    def from_ireq(cls, ireq): ...
    @classmethod
    def from_metadata(cls, name, version, extras, markers): ...
    @classmethod  
    def from_pipfile(cls, name, pipfile): ...
    def as_line(self, **kwargs): ...
    def as_pipfile(self): ...
    def as_ireq(self): ...

Requirement Processing

Pipfile Management

Read, manipulate, and work with Pipfile format used by Pipenv. Supports loading existing Pipfiles, accessing development and production dependencies, and extracting requirements in various formats.

class Pipfile:
    @classmethod
    def load(cls, path, create=False): ...
    @property
    def requirements(self): ...
    @property  
    def dev_requirements(self): ...
    def get_deps(self, dev=False, only=True): ...

Pipfile Management

Lockfile Operations

Work with Pipfile.lock files for reproducible dependency resolution. Load existing lockfiles, create new ones from Pipfiles, access locked dependencies, and convert to requirements.txt format.

class Lockfile:
    @classmethod
    def load(cls, path, create=True): ...
    @classmethod
    def from_data(cls, path, data, meta_from_project=True): ...
    def get_requirements(self, dev=True, only=False, categories=None): ...
    def as_requirements(self, category, include_hashes=False): ...
    def write(self): ...

Lockfile Operations

Exception Handling

Comprehensive exception classes for handling various error conditions during requirement parsing, file operations, and format conversions.

class RequirementError(Exception): ...
class MissingParameter(RequirementError): ...
class LockfileCorruptException(FileCorruptException): ...
class PipfileCorruptException(FileCorruptException): ...
class PipfileNotFound(FileNotFoundError): ...

Exception Handling

Utility Functions

Helper functions and constants for common requirement processing tasks including VCS detection, path validation, and package analysis.

# Constants
VCS_LIST = ("git", "svn", "hg", "bzr")
SCHEME_LIST = ("http://", "https://", "ftp://", "ftps://", "file://")

# Utility functions
def is_vcs(pipfile_entry): ...
def is_editable(pipfile_entry): ...
def strip_ssh_from_git_uri(uri): ...
def add_ssh_scheme_to_git_uri(uri): ...
def is_installable_file(path): ...
def get_setup_paths(base_path, subdirectory=None): ...
def prepare_pip_source_args(sources, pip_args=None): ...