Zope Event

raw JSON →
6.1 verified Tue May 12 auth: no python install: verified

The `zope.event` package provides a very basic, synchronous event publishing system. It enables application code to generate events without requiring awareness of the specific frameworks that might handle them. The current version is 6.1, released on 2025-11-07. Zope ecosystem packages typically follow semantic versioning, with minor releases approximately every 2-6 months and major releases every 2-3 years.

pip install zope.event
error ModuleNotFoundError: No module named 'zope.event'
cause The `zope.event` package is not installed in the current Python environment.
fix
pip install zope.event
error NameError: name 'notify' is not defined
cause The `notify` function was called without being imported from the `zope.event` package.
fix
from zope.event import notify
error ModuleNotFoundError: No module named 'zope.event.notify'
cause The `notify` function is an attribute of the `zope.event` module, not a submodule, so it cannot be imported directly using `import zope.event.notify`.
fix
from zope.event import notify
error AttributeError: module 'zope.event' has no attribute 'subscribe'
cause The `zope.event` module does not provide direct `subscribe` or `unsubscribe` functions; subscribers are typically registered by appending them to the `zope.event.subscribers` list or via `zope.component`.
fix
from zope.event import subscribers def my_handler(event): pass subscribers.append(my_handler)
breaking Version 6.0 replaced `pkg_resources` namespace with PEP 420 native namespaces, which might require `zc.buildout` version 5 if your project uses it.
fix Upgrade `zc.buildout` to version 5 if encountering namespace resolution issues, or ensure your build system is compatible with PEP 420 native namespaces.
breaking Version 6.1 dropped support for Python 3.9 and removed `setuptools` as a runtime dependency.
fix Ensure your project runs on Python 3.10 or newer. No action needed regarding `setuptools` unless you explicitly relied on it as a runtime dependency of `zope.event`.
breaking Version 5.0 dropped support for Python 2.7, 3.5, and 3.6. Version 5.1 further dropped support for Python 3.7 and 3.8.
fix Upgrade your Python environment to 3.10 or newer to use `zope.event` version 6.x.
gotcha The `zope.event.notify()` function is synchronous. It calls all registered subscribers in sequential order, and the notification process will block until all subscribers have completed their execution.
fix Design subscribers to be fast-executing. For long-running or I/O-bound operations, offload the work to a background task, thread, or process to prevent blocking the main application flow. Consider using a more advanced event system (like `zope.component` for type-based dispatching) or an asynchronous library if this behavior is problematic.
gotcha While direct manipulation of `zope.event.subscribers` (e.g., `zope.event.subscribers.append()`) is possible, it is generally discouraged for application-level event handling in favor of higher-level frameworks built on top of `zope.event`, such as `zope.component`.
fix For complex applications, especially those using `zope.interface` or `zope.component`, leverage their provided subscriber registration mechanisms (e.g., ZCML directives or programmatic adapters) rather than directly modifying the global `subscribers` list.
python os / libc status wheel install import disk
3.10 alpine (musl) wheel - 0.00s 17.8M
3.10 alpine (musl) - - 0.00s 17.8M
3.10 slim (glibc) wheel 1.5s 0.00s 18M
3.10 slim (glibc) - - 0.00s 18M
3.11 alpine (musl) wheel - 0.00s 19.6M
3.11 alpine (musl) - - 0.00s 19.6M
3.11 slim (glibc) wheel 1.6s 0.00s 20M
3.11 slim (glibc) - - 0.00s 20M
3.12 alpine (musl) wheel - 0.00s 11.5M
3.12 alpine (musl) - - 0.00s 11.5M
3.12 slim (glibc) wheel 1.4s 0.00s 12M
3.12 slim (glibc) - - 0.00s 12M
3.13 alpine (musl) wheel - 0.00s 11.3M
3.13 alpine (musl) - - 0.00s 11.2M
3.13 slim (glibc) wheel 1.4s 0.00s 12M
3.13 slim (glibc) - - 0.00s 12M
3.9 alpine (musl) wheel - 0.00s 17.3M
3.9 alpine (musl) - - 0.00s 17.3M
3.9 slim (glibc) wheel 1.8s 0.00s 18M
3.9 slim (glibc) - - 0.00s 18M

To use `zope.event`, you define an event object (which can be any Python object), define callable functions or methods as subscribers that accept the event object, and then add these subscribers to the `zope.event.subscribers` list. The `zope.event.notify()` function is then called with the event object, which dispatches it to all registered subscribers. The quickstart code temporarily clears the global subscriber list for isolation, which is not typical in a running application but useful for isolated examples or testing.

import zope.event

class MyEvent:
    def __init__(self, data):
        self.data = data

def my_subscriber(event):
    print(f"Subscriber 1 received event with data: {event.data}")

def another_subscriber(event):
    print(f"Subscriber 2 processed event: {event.data.upper()}")

# Clear existing subscribers for example isolation (optional for real apps)
# You might not want to do this in production, especially if other parts
# of your application register global subscribers.
_old_subscribers = zope.event.subscribers[:]
del zope.event.subscribers[:]

# Register subscribers
zope.event.subscribers.append(my_subscriber)
zope.event.subscribers.append(another_subscriber)

# Create and notify an event
event = MyEvent("hello world")
zope.event.notify(event)

# Unsubscribe (optional)
zope.event.subscribers.remove(my_subscriber)
zope.event.subscribers.remove(another_subscriber)

# Restore original subscribers (if applicable)
zope.event.subscribers.extend(_old_subscribers)

# Example of notifying with a simple object (any object can be an event)
zope.event.notify(42)
zope.event.notify("a string event")

print("\nDemonstrating direct manipulation of subscribers (for testing/setup):")
# Direct manipulation of the subscribers list is often used in tests
# or initialization, but higher-level frameworks like zope.component
# provide more sophisticated subscription mechanisms.
print(f"Current subscribers: {zope.event.subscribers}")