0
# Pipfile Management
1
2
The Pipfile class provides comprehensive functionality for reading, manipulating, and working with Pipfile format used by Pipenv. It supports loading existing Pipfiles, accessing development and production dependencies, and extracting requirements in various formats.
3
4
## Capabilities
5
6
### Pipfile Loading
7
8
Load and create Pipfile objects from filesystem paths with automatic project file detection and parsing.
9
10
```python { .api }
11
class Pipfile:
12
def __init__(self, path=None, projectfile=None, pipfile=None, **kwargs):
13
"""
14
Initialize Pipfile object.
15
16
Parameters:
17
- path: Path to Pipfile
18
- projectfile: ProjectFile instance
19
- pipfile: PipfileLoader instance
20
"""
21
22
@classmethod
23
def load(cls, path, create=False):
24
"""
25
Load Pipfile from path.
26
27
Parameters:
28
- path: Path to Pipfile
29
- create: Whether to create if not exists
30
31
Returns:
32
Pipfile object
33
"""
34
35
@classmethod
36
def read_projectfile(cls, path):
37
"""
38
Read project file from path.
39
40
Parameters:
41
- path: Path to project file
42
43
Returns:
44
ProjectFile object
45
"""
46
47
@classmethod
48
def load_projectfile(cls, path, create=False):
49
"""
50
Load or create project file.
51
52
Parameters:
53
- path: Path to project file
54
- create: Whether to create if not exists
55
56
Returns:
57
ProjectFile object
58
"""
59
```
60
61
### Core Properties
62
63
Access Pipfile metadata, paths, and configuration information.
64
65
```python { .api }
66
class Pipfile:
67
@property
68
def path(self) -> Path:
69
"""Path to Pipfile"""
70
71
@property
72
def projectfile(self) -> ProjectFile:
73
"""Project file instance"""
74
75
@property
76
def pipfile(self) -> Optional[PipfileLoader]:
77
"""Internal pipfile loader"""
78
79
@property
80
def root(self) -> Path:
81
"""Project root directory"""
82
83
@property
84
def requires_python(self) -> bool:
85
"""Python version requirement"""
86
87
@property
88
def allow_prereleases(self) -> bool:
89
"""Whether prereleases allowed"""
90
91
@property
92
def extended_keys(self) -> List:
93
"""Extended package section keys"""
94
95
build_system: Optional[Dict]
96
"""Optional build system configuration from pyproject.toml"""
97
```
98
99
### Dependency Access
100
101
Access and iterate over production and development dependencies as Requirement objects.
102
103
```python { .api }
104
class Pipfile:
105
@property
106
def packages(self) -> List[Requirement]:
107
"""Production requirements list"""
108
109
@property
110
def dev_packages(self) -> List[Requirement]:
111
"""Development requirements list"""
112
113
@property
114
def requirements(self) -> List[Requirement]:
115
"""Production requirements (alias for packages)"""
116
117
@property
118
def dev_requirements(self) -> List[Requirement]:
119
"""Development requirements (alias for dev_packages)"""
120
121
def get_deps(self, dev=False, only=True):
122
"""
123
Get dependencies dictionary.
124
125
Parameters:
126
- dev: Include development dependencies
127
- only: Only return specified category
128
129
Returns:
130
Dict of dependencies
131
"""
132
```
133
134
### Data Access
135
136
Access Pipfile data using dictionary-like interface for direct manipulation.
137
138
```python { .api }
139
class Pipfile:
140
def get(self, k):
141
"""
142
Get item by key.
143
144
Parameters:
145
- k: Key to retrieve
146
147
Returns:
148
Value for key
149
"""
150
151
def __contains__(self, k):
152
"""
153
Check if key exists.
154
155
Parameters:
156
- k: Key to check
157
158
Returns:
159
bool: True if key exists
160
"""
161
162
def __getitem__(self, k, *args, **kwargs):
163
"""
164
Get item by key using bracket notation.
165
166
Parameters:
167
- k: Key to retrieve
168
169
Returns:
170
Value for key
171
"""
172
173
def __getattr__(self, k, *args, **kwargs):
174
"""
175
Get attribute by name.
176
177
Parameters:
178
- k: Attribute name
179
180
Returns:
181
Attribute value
182
"""
183
```
184
185
## Usage Examples
186
187
### Loading Pipfiles
188
189
```python
190
from requirementslib import Pipfile
191
192
# Load existing Pipfile
193
pipfile = Pipfile.load("./Pipfile")
194
195
# Load Pipfile with creation if missing
196
pipfile = Pipfile.load("./Pipfile", create=True)
197
198
# Load from specific path
199
pipfile = Pipfile.load("/path/to/project/Pipfile")
200
```
201
202
### Accessing Dependencies
203
204
```python
205
from requirementslib import Pipfile
206
207
# Load Pipfile
208
pipfile = Pipfile.load("./Pipfile")
209
210
# Access production dependencies
211
for req in pipfile.requirements:
212
print(f"Production: {req.name} {req.specifiers}")
213
214
# Access development dependencies
215
for req in pipfile.dev_requirements:
216
print(f"Development: {req.name} {req.specifiers}")
217
218
# Get dependencies as dictionary
219
prod_deps = pipfile.get_deps(dev=False, only=True)
220
dev_deps = pipfile.get_deps(dev=True, only=True)
221
222
print("Production dependencies:", prod_deps)
223
print("Development dependencies:", dev_deps)
224
```
225
226
### Working with Requirements
227
228
```python
229
from requirementslib import Pipfile
230
231
# Load Pipfile
232
pipfile = Pipfile.load("./Pipfile")
233
234
# Convert requirements to different formats
235
for req in pipfile.requirements:
236
# Convert to requirements.txt format
237
req_line = req.as_line()
238
print(f"requirements.txt: {req_line}")
239
240
# Convert to Pipfile format
241
pipfile_entry = req.as_pipfile()
242
print(f"Pipfile entry: {pipfile_entry}")
243
244
# Convert to pip InstallRequirement
245
ireq = req.as_ireq()
246
print(f"InstallRequirement: {ireq}")
247
```
248
249
### Pipfile Properties
250
251
```python
252
from requirementslib import Pipfile
253
254
# Load Pipfile
255
pipfile = Pipfile.load("./Pipfile")
256
257
# Access Pipfile properties
258
print(f"Pipfile path: {pipfile.path}")
259
print(f"Project root: {pipfile.root}")
260
print(f"Requires Python: {pipfile.requires_python}")
261
print(f"Allow prereleases: {pipfile.allow_prereleases}")
262
263
# Access underlying data
264
if "source" in pipfile:
265
sources = pipfile["source"]
266
print(f"Package sources: {sources}")
267
268
if "scripts" in pipfile:
269
scripts = pipfile["scripts"]
270
print(f"Scripts: {scripts}")
271
```
272
273
### Working with Editable Dependencies
274
275
```python
276
from requirementslib import Pipfile
277
278
# Load Pipfile
279
pipfile = Pipfile.load("./Pipfile")
280
281
# Find editable dependencies
282
for req in pipfile.requirements + pipfile.dev_requirements:
283
if req.editable:
284
print(f"Editable package: {req.name}")
285
print(f" Path/URL: {req.req}")
286
print(f" VCS: {req.vcs if req.is_vcs else 'local path'}")
287
288
# Find VCS dependencies
289
for req in pipfile.requirements + pipfile.dev_requirements:
290
if req.is_vcs:
291
print(f"VCS package: {req.name}")
292
print(f" VCS type: {req.vcs}")
293
print(f" Commit hash: {req.commit_hash}")
294
```