Environ Global State Management
`environ` is a small Python library providing a stack-based mechanism for managing global application state. It allows setting and retrieving values globally, with the ability to temporarily override values within a context manager. The library's latest version is 1.0, released in 2012, and it is no longer actively maintained or supported.
Common errors
-
ModuleNotFoundError: No module named 'environ'
cause The 'environ' package has not been installed.fixRun `pip install environ` to install the package. -
TypeError: 'Environ' object is not callable
cause The `environ` object is a singleton instance for managing state, not a constructor to be called like `environ()`.fixAccess methods directly on the `environ` object, e.g., `environ.set('key', value)` or `environ.get('key')`. -
TypeError: 'Environ' object does not support item assignment
cause The `environ` object is not a dictionary and does not support direct item assignment or access (e.g., `environ['key'] = value` or `value = environ['key']`).fixUse the dedicated `set` and `get` methods: `environ.set('key', value)` and `value = environ.get('key')`.
Warnings
- breaking This library is **abandoned** and has not been updated since its 1.0 release in 2012. It is not officially supported on modern Python versions (e.g., Python 3.8+). While it might still function for simple cases, compatibility issues or unexpected behavior are likely to arise. Consider alternative solutions for global state management or environment variables.
- gotcha The library's name `environ` is highly misleading. It is **not** designed for managing operating system environment variables (like `PATH` or `HOME`). Instead, it manages application-specific global state. For OS environment variables, use Python's built-in `os.environ`.
- gotcha `environ` uses a global singleton object, which can make testing and managing application state challenging. Modifying global state can lead to side effects, make tests difficult to isolate, and introduce hard-to-debug issues in complex applications.
Install
-
pip install environ
Imports
- environ
from environ import environ
Quickstart
from environ import environ
environ.set('app_config_key', 'initial_value')
assert environ.get('app_config_key') == 'initial_value'
# Temporarily override values within a context
with environ(temporary_key='context_value'):
assert environ.get('app_config_key') == 'initial_value'
assert environ.get('temporary_key') == 'context_value'
environ.set('another_context_key', 'inside_context')
assert environ.get('another_context_key') == 'inside_context'
# Outside the context, temporary values are reverted
assert environ.get('app_config_key') == 'initial_value'
assert environ.get('temporary_key') is None
assert environ.get('another_context_key') is None