Monty (Python Complement Library)
Monty is a Python library that acts as a missing complement to the Python standard library. It provides supplementary useful functions, including transparent support for zipped files, various design patterns like singletons and cached_class, and other utilities to simplify common programming tasks. The current version is 2026.2.18, and it maintains an active release cadence, often aligning with needs from scientific frameworks it supports.
Warnings
- gotcha Monty aims to complement the standard library, but be mindful of potential name collisions if you import specific functions that share names with standard library functions (e.g., `open`). Always prefer qualified imports (`monty.io.zopen`) or aliased imports (`import monty as mty`) to avoid unexpected behavior.
- deprecated The `monty.dev` module contains a `deprecated` decorator. While this is a utility provided by Monty, using it on your own code implies maintaining deprecation warnings. Ensure you understand its usage, especially when migrating or updating your code to remove deprecated features.
- gotcha There are several Python libraries published under or related to the name 'monty' (e.g., 'pydantic-monty', 'boppreh/monty', 'libmonty'). Ensure you are installing and using the correct 'monty' library (from `materialsvirtuallab/monty`) for the intended functionalities described as 'missing complement to Python'.
- breaking Older documentation mentions Monty supporting Python 2.7-3.x. However, PyPI metadata for the current version (2026.2.18) clearly states `requires_python: >=3.11`. Attempting to use recent versions of Monty with older Python 3 releases (e.g., 3.9, 3.10) will likely result in installation failures or runtime errors.
- breaking In `monty` version 1.0.0, `ruamel.yaml` became the default YAML parser and dumper. If your project previously relied on specific behaviors or features of `PyYAML` (which might have been the default prior to this change) when handling YAML files through Monty, this change could introduce subtle breaking changes in how YAML data is serialized or deserialized.
Install
-
pip install monty
Imports
- monty
import monty as mty
- zopen
from monty.io import zopen
Quickstart
import monty.io
# Example: Transparently open compressed or uncompressed files
# Create a dummy gzipped file
with open('example.txt', 'w') as f:
f.write('Hello, Monty!\n')
import gzip
with open('example.txt', 'rb') as f_in:
with gzip.open('example.gz', 'wb') as f_out:
f_out.writelines(f_in)
# Use monty.io.zopen to read from the gzipped file
with monty.io.zopen('example.gz', 'rt') as f:
content = f.read()
print(f'Content from gzipped file: {content.strip()}')
# Use monty.io.zopen to write to a bzipped file
with monty.io.zopen('output.bz2', 'wt') as f:
f.write('This is written to a bzip2 file using monty.io.zopen.')
# Verify by reading back
with monty.io.zopen('output.bz2', 'rt') as f:
read_content = f.read()
print(f'Content from bzipped file: {read_content.strip()}')
import os
os.remove('example.txt')
os.remove('example.gz')
os.remove('output.bz2')