A tool for converting between pip-style and pipfile requirements.
npx @tessl/cli install tessl/pypi-requirementslib@3.0.0A 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.
pip install requirementslibfrom requirementslib import Requirement, Pipfile, Lockfile
from requirementslib.utils import is_vcs, is_editable, VCS_LISTfrom 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)RequirementsLib is built around three main classes that handle different aspects of Python dependency management:
The library provides seamless conversion between formats while preserving all metadata including version constraints, environment markers, VCS requirements, editable installs, and package hashes.
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): ...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): ...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): ...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): ...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): ...