Plette
Plette is a Python library that provides structured models for parsing, generating, and validating Pipfile and Pipfile.lock files. It allows developers to programmatically interact with project dependencies in a structured manner. The current version is 2.1.0, and it maintains a regular release cadence with significant updates, including a major version bump from v1 to v2.
Common errors
-
AttributeError: 'Pipfile' object has no attribute 'missing_section'
cause Attempting to access a non-existent top-level section in a Pipfile object directly as an attribute.fixUse dictionary-style `.get()` method to safely access sections, providing a default value if the section might be missing. E.g., `pipfile.get('missing_section', {})`. -
KeyError: 'some-package' when accessing lockfile.default['some-package']
cause The specified package 'some-package' is not found within the 'default' dependencies section of the loaded Lockfile.fixVerify the package name is correct and present in the `Pipfile.lock`. Use `lockfile.default.get('some-package')` to handle cases where the package might not exist without raising an error. -
TypeError: argument of type 'Lockfile' is not iterable
cause Trying to iterate directly over a `Lockfile` object, which is not designed to be iterable.fixAccess specific sections of the `Lockfile` (e.g., `lockfile.default`, `lockfile.develop`) and iterate over those dictionaries. For example, `for package, details in lockfile.default.items(): ...`.
Warnings
- gotcha Accessing missing sections or keys in Pipfile or Lockfile models directly (e.g., `pipfile.non_existent_section`) will raise `KeyError` or `AttributeError`.
- deprecated While canonical names like `pipfile.source` and `lockfile._meta` are still available, direct manipulation of the `_meta` section of a `Lockfile` is generally discouraged.
- breaking Major version `2.0.0` was released. While specific migration notes for `plette` are not extensively documented, changes between major versions often involve API refinements. Users upgrading from v1.x may encounter breaking changes in how certain properties are accessed or how objects are constructed.
Install
-
pip install plette
Imports
- Pipfile
import plette.Pipfile_deprecated
from plette import Pipfile
- Lockfile
import plette.Lockfile_util
from plette import Lockfile
Quickstart
import io
from plette import Pipfile, Lockfile
# Example Pipfile content
pipfile_content = '''
[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"
[packages]
requests = "*"
[dev-packages]
pytest = "*"
[requires]
python_version = "3.8"
'''
# Example Pipfile.lock content (simplified for demonstration)
lockfile_content = '''
{
"_meta": {
"hash": {
"sha256": "some_hash"
},
"requires": {
"python_version": "3.8"
}
},
"default": {
"requests": {
"hashes": [
"sha256:hash1",
"sha256:hash2"
],
"version": "==2.28.1"
}
},
"develop": {
"pytest": {
"hashes": [
"sha256:hash3",
"sha256:hash4"
],
"version": "==7.1.2"
}
}
}
'''
# Load Pipfile
with io.StringIO(pipfile_content) as f:
pipfile = Pipfile.load(f)
print(f"Pipfile Python Version: {pipfile.requires.python_version}")
print(f"Pipfile Packages: {list(pipfile.packages.keys())}")
# Load Lockfile
with io.StringIO(lockfile_content) as f:
lockfile = Lockfile.load(f)
print(f"Lockfile requests version: {lockfile.default['requests']['version']}")
print(f"Lockfile meta hash: {lockfile._meta['hash']['sha256']}")
# Modify and dump (Pipfile example)
pipfile.packages['rich'] = '==12.0.0'
with io.StringIO() as f:
pipfile.dump(f)
print("\nModified Pipfile content:")
print(f.getvalue())