Pipfile File Format Specification Library
The `pipfile` library serves as the canonical reference and parsing implementation for the Pipfile and Pipfile.lock file formats. It is a low-level library primarily intended for use by other tools (such as Pipenv) to programmatically read, write, and manipulate these dependency definition files. The current version is 0.0.2, and its release cadence is very infrequent, reflecting its role as a stable specification parser rather than a frequently updated application.
Common errors
-
ModuleNotFoundError: No module named 'pipfile'
cause The `pipfile` package has not been installed in your current Python environment.fixInstall the package using pip: `pip install pipfile` -
AttributeError: module 'pipfile' has no attribute 'load_from_path'
cause The `pipfile` module does not expose a top-level `load_from_path` or similar direct utility function. Parsing is typically done via static methods on the `Pipfile` or `PipfileLock` classes.fixUse `from pipfile import Pipfile` then `pipfile_obj = Pipfile.load('/path/to/Pipfile')` for loading from a file, or `pipfile_obj = Pipfile.parse(content_string)` for parsing from a string. -
TypeError: 'Pipfile' object is not callable
cause You are attempting to instantiate the `Pipfile` class directly with a path (e.g., `Pipfile('Pipfile')`) when its constructor is not designed for this. Instead, use its static `load` or `parse` methods.fixTo load from a file, use `pipfile_obj = Pipfile.load('/path/to/Pipfile')`. To parse from a string, use `pipfile_obj = Pipfile.parse(content_string)`.
Warnings
- gotcha This library (`pipfile`) is the specification parser for Pipfiles and Pipfile.lock, not the end-user command-line tool. If you are looking to manage Python project dependencies, install packages, and manage virtual environments, you should use `pipenv`.
- gotcha The `pipfile` library has an extremely low release cadence (current version 0.0.2 from 2017). While the underlying Pipfile specification is actively maintained, this specific library is considered stable for its current parsing capabilities and is unlikely to receive frequent new features or updates.
Install
-
pip install pipfile
Imports
- Pipfile
from pipfile import Pipfile
- PipfileLock
from pipfile import PipfileLock
Quickstart
import os
from pipfile import Pipfile, PipfileLock
# Example Pipfile content (simulating reading from a file)
pipfile_content = """
[packages]
requests = "*"
flask = {version = "==2.0.0", extras = ["dotenv"]}
[dev-packages]
pytest = "*"
[requires]
python_version = "3.9"
"""
# Parse a Pipfile from a string
# For real files, use Pipfile.load('/path/to/Pipfile')
pipfile = Pipfile.parse(pipfile_content)
print("--- Parsed Pipfile ---")
print(f"Python version required: {pipfile.requires.get('python_version')}")
print("Packages (default):")
for name, spec in pipfile.packages.items():
print(f" {name}: {spec}")
print("Dev Packages:")
for name, spec in pipfile.dev_packages.items():
print(f" {name}: {spec}")
# Example Pipfile.lock content (simulating reading from a file)
# (Simplified structure for demonstration)
lock_content = """
{
"_meta": {
"hash": {
"sha256": "..."
},
"pipfile-spec": 6,
"requires": {
"python_version": "3.9"
}
},
"default": {
"requests": {
"hashes": ["sha256:..."],
"version": "==2.28.1"
},
"flask": {
"hashes": ["sha256:..."],
"version": "==2.0.0"
}
},
"develop": {
"pytest": {
"hashes": ["sha256:..."],
"version": "==7.1.2"
}
}
}
"""
# Parse a Pipfile.lock from a string
# For real files, use PipfileLock.load('/path/to/Pipfile.lock')
pipfile_lock = PipfileLock.parse(lock_content)
print("\n--- Parsed Pipfile.lock ---")
print("Locked packages (default):")
for name, details in pipfile_lock.default_packages.items():
print(f" {name}: {details.get('version')}")