BagIt
raw JSON → 1.9.0 verified Mon Apr 27 auth: no python
Create and validate BagIt packages, a hierarchical file packaging format for digital preservation. This library supports BagIt version 0.97 and offers strong validation. Current stable release is 1.9.0 (released March 2023). It is mature and well-maintained.
pip install bagit Common errors
error ModuleNotFoundError: No module named 'bagit' ↓
cause The library is not installed or installed in a different Python environment.
fix
Run 'pip install bagit' in the correct Python environment (check with which python or python -m pip).
error ValueError: No manifest declarations for ... but file found in payload. ↓
cause The payload directory contains files not listed in the manifest.
fix
Recreate the bag with bagit.make_bag() or manually add entries to manifest-*.txt.
error bagit.BagError: Bag validation failed: missing expected file ... ↓
cause A file declared in the manifest is missing from the payload directory.
fix
Restore the missing file or regenerate the bag.
error TypeError: make_bag() got an unexpected keyword argument 'checksums' ↓
cause Using an older version of bagit (<1.5) that does not support the checksums parameter.
fix
Update to latest bagit: pip install --upgrade bagit
Warnings
breaking In bagit 1.8.0+, the default checksum algorithm changed from MD5 to SHA256. Scripts relying on MD5 must explicitly pass checksums=['md5']. ↓
fix When making a bag, specify checksums=['md5'] if you need backward compatibility with older tools.
gotcha The validate_bag function modifies the bag in place by writing fetch.txt if any file is missing from the payload. This is unexpected and can cause side effects. ↓
fix Use validate_bag(..., fast=True) to skip remote file validation, or use check_completeness() separately.
gotcha Paths in bagit are case-sensitive on Linux but not on macOS/Windows. This can cause validation failures when moving bags between systems. ↓
fix Ensure all file paths within the bag use consistent casing.
deprecated Python 2 support ended in bagit 1.9.0. If you need Python 2 compatibility, use bagit < 1.7.0. ↓
fix Upgrade to Python 3, or pin bagit to 1.6.0 for Python 2.
Imports
- bagit wrong
from bagit import Bagcorrectimport bagit - Bag wrong
import bagit.Bagcorrectfrom bagit import Bag
Quickstart
import bagit
import os
# Create a bag
bag_path = '/tmp/mybag'
os.makedirs(bag_path, exist_ok=True)
with open(os.path.join(bag_path, 'test.txt'), 'w') as f:
f.write('hello')
bag = bagit.make_bag(bag_path, checksums=['md5', 'sha256'])
print('Bag created:', bag)
# Validate an existing bag
valid, messages = bagit.validate_bag(bag_path)
if valid:
print('Bag is valid')
else:
print('Errors:', messages)