{"id":5328,"library":"monty","title":"Monty (Python Complement Library)","description":"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.","status":"active","version":"2026.2.18","language":"en","source_language":"en","source_url":"https://github.com/materialsvirtuallab/monty","tags":["utility","python-complement","file-handling","design-patterns","scientific-computing"],"install":[{"cmd":"pip install monty","lang":"bash","label":"Install latest version"}],"dependencies":[],"imports":[{"note":"The official documentation suggests importing the root `monty` module and using it as an alias (e.g., `mty`) to access its various sub-modules and functions, mirroring the standard Python library's structure.","wrong":"from monty.io import zopen # While technically correct, 'import monty as mty' is the recommended usage pattern to access its modules and avoid potential name clashes.","symbol":"monty","correct":"import monty as mty"},{"note":"Many of Monty's utilities are organized into submodules (e.g., `io`, `dev`). Direct import from the root `monty` package for specific functions like `zopen` is incorrect; you must specify the submodule.","wrong":"from monty import zopen","symbol":"zopen","correct":"from monty.io import zopen"}],"quickstart":{"code":"import monty.io\n\n# Example: Transparently open compressed or uncompressed files\n# Create a dummy gzipped file\nwith open('example.txt', 'w') as f:\n    f.write('Hello, Monty!\\n')\n\nimport gzip\nwith open('example.txt', 'rb') as f_in:\n    with gzip.open('example.gz', 'wb') as f_out:\n        f_out.writelines(f_in)\n\n# Use monty.io.zopen to read from the gzipped file\nwith monty.io.zopen('example.gz', 'rt') as f:\n    content = f.read()\n    print(f'Content from gzipped file: {content.strip()}')\n\n# Use monty.io.zopen to write to a bzipped file\nwith monty.io.zopen('output.bz2', 'wt') as f:\n    f.write('This is written to a bzip2 file using monty.io.zopen.')\n\n# Verify by reading back\nwith monty.io.zopen('output.bz2', 'rt') as f:\n    read_content = f.read()\n    print(f'Content from bzipped file: {read_content.strip()}')\n\nimport os\nos.remove('example.txt')\nos.remove('example.gz')\nos.remove('output.bz2')","lang":"python","description":"This quickstart demonstrates `monty.io.zopen`, a utility that transparently handles compressed (gzip, bzip2) or uncompressed files based on their filename extension. It functions similarly to Python's built-in `open`, abstracting away the compression details."},"warnings":[{"fix":"Use `import monty as mty` and access functions via `mty.module.function()` or `from monty.module import function` for clarity and to prevent shadowing standard library names.","message":"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.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Regularly review your codebase for deprecated functions or classes marked by `monty.dev.deprecated`. Update your code to use the recommended replacements to avoid future compatibility issues and maintain clean warning logs.","message":"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.","severity":"deprecated","affected_versions":"All versions"},{"fix":"Always verify the PyPI package name (`monty`) and the project's official source or documentation (e.g., `materialsvirtuallab/monty` on GitHub) to confirm you are using the intended library. Check import paths as different 'monty' libraries will have different top-level packages.","message":"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'.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Ensure your Python environment is version 3.11 or newer to be compatible with the latest `monty` releases. Check the `requires_python` field on PyPI for specific version constraints.","message":"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.","severity":"breaking","affected_versions":">=2026.2.18"},{"fix":"If upgrading from very old versions of `monty` (pre-1.0.0) that used a different YAML backend, review YAML serialization/deserialization logic. Test your application's YAML handling thoroughly to ensure compatibility with `ruamel.yaml`'s behavior.","message":"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.","severity":"breaking","affected_versions":"1.0.0 and later (prior to 2026.2.18)"}],"env_vars":null,"last_verified":"2026-04-13T00:00:00.000Z","next_check":"2026-07-12T00:00:00.000Z"}