A tool for converting between pip-style and pipfile requirements.
—
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.
Load and create Pipfile objects from filesystem paths with automatic project file detection and parsing.
class Pipfile:
def __init__(self, path=None, projectfile=None, pipfile=None, **kwargs):
"""
Initialize Pipfile object.
Parameters:
- path: Path to Pipfile
- projectfile: ProjectFile instance
- pipfile: PipfileLoader instance
"""
@classmethod
def load(cls, path, create=False):
"""
Load Pipfile from path.
Parameters:
- path: Path to Pipfile
- create: Whether to create if not exists
Returns:
Pipfile object
"""
@classmethod
def read_projectfile(cls, path):
"""
Read project file from path.
Parameters:
- path: Path to project file
Returns:
ProjectFile object
"""
@classmethod
def load_projectfile(cls, path, create=False):
"""
Load or create project file.
Parameters:
- path: Path to project file
- create: Whether to create if not exists
Returns:
ProjectFile object
"""Access Pipfile metadata, paths, and configuration information.
class Pipfile:
@property
def path(self) -> Path:
"""Path to Pipfile"""
@property
def projectfile(self) -> ProjectFile:
"""Project file instance"""
@property
def pipfile(self) -> Optional[PipfileLoader]:
"""Internal pipfile loader"""
@property
def root(self) -> Path:
"""Project root directory"""
@property
def requires_python(self) -> bool:
"""Python version requirement"""
@property
def allow_prereleases(self) -> bool:
"""Whether prereleases allowed"""
@property
def extended_keys(self) -> List:
"""Extended package section keys"""
build_system: Optional[Dict]
"""Optional build system configuration from pyproject.toml"""Access and iterate over production and development dependencies as Requirement objects.
class Pipfile:
@property
def packages(self) -> List[Requirement]:
"""Production requirements list"""
@property
def dev_packages(self) -> List[Requirement]:
"""Development requirements list"""
@property
def requirements(self) -> List[Requirement]:
"""Production requirements (alias for packages)"""
@property
def dev_requirements(self) -> List[Requirement]:
"""Development requirements (alias for dev_packages)"""
def get_deps(self, dev=False, only=True):
"""
Get dependencies dictionary.
Parameters:
- dev: Include development dependencies
- only: Only return specified category
Returns:
Dict of dependencies
"""Access Pipfile data using dictionary-like interface for direct manipulation.
class Pipfile:
def get(self, k):
"""
Get item by key.
Parameters:
- k: Key to retrieve
Returns:
Value for key
"""
def __contains__(self, k):
"""
Check if key exists.
Parameters:
- k: Key to check
Returns:
bool: True if key exists
"""
def __getitem__(self, k, *args, **kwargs):
"""
Get item by key using bracket notation.
Parameters:
- k: Key to retrieve
Returns:
Value for key
"""
def __getattr__(self, k, *args, **kwargs):
"""
Get attribute by name.
Parameters:
- k: Attribute name
Returns:
Attribute value
"""from requirementslib import Pipfile
# Load existing Pipfile
pipfile = Pipfile.load("./Pipfile")
# Load Pipfile with creation if missing
pipfile = Pipfile.load("./Pipfile", create=True)
# Load from specific path
pipfile = Pipfile.load("/path/to/project/Pipfile")from requirementslib import Pipfile
# Load Pipfile
pipfile = Pipfile.load("./Pipfile")
# Access production dependencies
for req in pipfile.requirements:
print(f"Production: {req.name} {req.specifiers}")
# Access development dependencies
for req in pipfile.dev_requirements:
print(f"Development: {req.name} {req.specifiers}")
# Get dependencies as dictionary
prod_deps = pipfile.get_deps(dev=False, only=True)
dev_deps = pipfile.get_deps(dev=True, only=True)
print("Production dependencies:", prod_deps)
print("Development dependencies:", dev_deps)from requirementslib import Pipfile
# Load Pipfile
pipfile = Pipfile.load("./Pipfile")
# Convert requirements to different formats
for req in pipfile.requirements:
# Convert to requirements.txt format
req_line = req.as_line()
print(f"requirements.txt: {req_line}")
# Convert to Pipfile format
pipfile_entry = req.as_pipfile()
print(f"Pipfile entry: {pipfile_entry}")
# Convert to pip InstallRequirement
ireq = req.as_ireq()
print(f"InstallRequirement: {ireq}")from requirementslib import Pipfile
# Load Pipfile
pipfile = Pipfile.load("./Pipfile")
# Access Pipfile properties
print(f"Pipfile path: {pipfile.path}")
print(f"Project root: {pipfile.root}")
print(f"Requires Python: {pipfile.requires_python}")
print(f"Allow prereleases: {pipfile.allow_prereleases}")
# Access underlying data
if "source" in pipfile:
sources = pipfile["source"]
print(f"Package sources: {sources}")
if "scripts" in pipfile:
scripts = pipfile["scripts"]
print(f"Scripts: {scripts}")from requirementslib import Pipfile
# Load Pipfile
pipfile = Pipfile.load("./Pipfile")
# Find editable dependencies
for req in pipfile.requirements + pipfile.dev_requirements:
if req.editable:
print(f"Editable package: {req.name}")
print(f" Path/URL: {req.req}")
print(f" VCS: {req.vcs if req.is_vcs else 'local path'}")
# Find VCS dependencies
for req in pipfile.requirements + pipfile.dev_requirements:
if req.is_vcs:
print(f"VCS package: {req.name}")
print(f" VCS type: {req.vcs}")
print(f" Commit hash: {req.commit_hash}")Install with Tessl CLI
npx tessl i tessl/pypi-requirementslib