0
# RequirementsLib
1
2
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.
3
4
## Package Information
5
6
- **Package Name**: requirementslib
7
- **Language**: Python
8
- **Installation**: `pip install requirementslib`
9
10
## Core Imports
11
12
```python
13
from requirementslib import Requirement, Pipfile, Lockfile
14
from requirementslib.utils import is_vcs, is_editable, VCS_LIST
15
```
16
17
## Basic Usage
18
19
```python
20
from requirementslib import Requirement, Pipfile, Lockfile
21
22
# Parse a requirement from a requirements.txt line
23
req = Requirement.from_line("requests>=2.25.0")
24
print(req.name) # "requests"
25
print(req.specifiers) # ">=2.25.0"
26
27
# Convert to different formats
28
print(req.as_line()) # "requests>=2.25.0"
29
pipfile_entry = req.as_pipfile() # {"requests": ">=2.25.0"}
30
31
# Load and work with Pipfiles
32
pipfile = Pipfile.load("./Pipfile")
33
for req in pipfile.requirements:
34
print(f"{req.name}: {req.specifiers}")
35
36
# Load and work with lockfiles
37
lockfile = Lockfile.load("./Pipfile.lock")
38
for req in lockfile.get_requirements():
39
print(f"{req.name}: {req.specifiers}")
40
41
# Convert lockfile requirements to requirements.txt format
42
requirements_lines = lockfile.as_requirements("default")
43
for line in requirements_lines:
44
print(line)
45
```
46
47
## Architecture
48
49
RequirementsLib is built around three main classes that handle different aspects of Python dependency management:
50
51
- **Requirement**: Core class for individual package requirements with extensive parsing and conversion capabilities
52
- **Pipfile**: Interface for reading and manipulating Pipfile format used by Pipenv
53
- **Lockfile**: Interface for reading and manipulating Pipfile.lock format for reproducible builds
54
55
The library provides seamless conversion between formats while preserving all metadata including version constraints, environment markers, VCS requirements, editable installs, and package hashes.
56
57
## Capabilities
58
59
### Requirement Processing
60
61
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.
62
63
```python { .api }
64
class Requirement:
65
@classmethod
66
def from_line(cls, line, parse_setup_info=True): ...
67
@classmethod
68
def from_ireq(cls, ireq): ...
69
@classmethod
70
def from_metadata(cls, name, version, extras, markers): ...
71
@classmethod
72
def from_pipfile(cls, name, pipfile): ...
73
def as_line(self, **kwargs): ...
74
def as_pipfile(self): ...
75
def as_ireq(self): ...
76
```
77
78
[Requirement Processing](./requirement.md)
79
80
### Pipfile Management
81
82
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.
83
84
```python { .api }
85
class Pipfile:
86
@classmethod
87
def load(cls, path, create=False): ...
88
@property
89
def requirements(self): ...
90
@property
91
def dev_requirements(self): ...
92
def get_deps(self, dev=False, only=True): ...
93
```
94
95
[Pipfile Management](./pipfile.md)
96
97
### Lockfile Operations
98
99
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.
100
101
```python { .api }
102
class Lockfile:
103
@classmethod
104
def load(cls, path, create=True): ...
105
@classmethod
106
def from_data(cls, path, data, meta_from_project=True): ...
107
def get_requirements(self, dev=True, only=False, categories=None): ...
108
def as_requirements(self, category, include_hashes=False): ...
109
def write(self): ...
110
```
111
112
[Lockfile Operations](./lockfile.md)
113
114
### Exception Handling
115
116
Comprehensive exception classes for handling various error conditions during requirement parsing, file operations, and format conversions.
117
118
```python { .api }
119
class RequirementError(Exception): ...
120
class MissingParameter(RequirementError): ...
121
class LockfileCorruptException(FileCorruptException): ...
122
class PipfileCorruptException(FileCorruptException): ...
123
class PipfileNotFound(FileNotFoundError): ...
124
```
125
126
[Exception Handling](./exceptions.md)
127
128
### Utility Functions
129
130
Helper functions and constants for common requirement processing tasks including VCS detection, path validation, and package analysis.
131
132
```python { .api }
133
# Constants
134
VCS_LIST = ("git", "svn", "hg", "bzr")
135
SCHEME_LIST = ("http://", "https://", "ftp://", "ftps://", "file://")
136
137
# Utility functions
138
def is_vcs(pipfile_entry): ...
139
def is_editable(pipfile_entry): ...
140
def strip_ssh_from_git_uri(uri): ...
141
def add_ssh_scheme_to_git_uri(uri): ...
142
def is_installable_file(path): ...
143
def get_setup_paths(base_path, subdirectory=None): ...
144
def prepare_pip_source_args(sources, pip_args=None): ...
145
```