Registry / devops / watchdog

watchdog

JSON →
library6.0.0pypypi✓ verified 25d ago

Watchdog is a Python library and shell utility that provides an API for monitoring file system events in real-time. It supports various operating systems by utilizing native APIs like inotify (Linux), FSEvents (macOS), and ReadDirectoryChangesW (Windows), falling back to polling when native APIs are unavailable. Currently at version 6.0.0, the library maintains an active development pace with major releases approximately annually.

pip install watchdog
INSTALL
IMPORT
SIG · WATCHDOG
W
watchdog
devopspythonv6.0.0
Install
1.9s avg
Import
116ms
Disk
19MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v6.0.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.910 runs
installs and imports cleanly · install 0.0s · import 0.118s · 20.5MB
glibc
py 3.103.910 runs
installs and imports cleanly · install 1.9s · import 0.113s · 22MB
19MB installed
● package 19MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Observer
from watchdog.observers import Observer
FileSystemEventHandler
from watchdog.events import FileSystemEventHandler
LoggingEventHandler
from watchdog.events import LoggingEventHandler
A built-in event handler for simple logging.

This quickstart monitors the current directory ('.') recursively for any filesystem events using a custom event handler and prints them to the console. The observer runs in a separate thread until a KeyboardInterrupt is received.

import time from watchdog.observers import Observer from watchdog.events import FileSystemEventHandler class MyEventHandler(FileSystemEventHandler): def on_any_event(self, event): # Log any event for demonstration print(f"Event type: {event.event_type} | Path: {event.src_path} | Is directory: {event.is_directory}") if __name__ == "__main__": path_to_watch = "." event_handler = MyEventHandler() observer = Observer() observer.schedule(event_handler, path_to_watch, recursive=True) observer.start() try: print(f"Monitoring directory: {path_to_watch}") while True: time.sleep(1) except KeyboardInterrupt: observer.stop() observer.join()
watchdog --version
Debug
Known issues
breakingPython 3.8 support was dropped in Watchdog v5.0.0. Python 3.7 support was dropped in v4.0.0. Ensure your Python environment is 3.9 or newer.
fix
Upgrade your Python interpreter to version 3.9 or later.
affects: >=5.0.0, >=4.0.0
breakingIn v5.0.0, several core classes and exceptions were renamed (e.g., `BaseObserverSubclassCallable` to `ObserverType`, `UnsupportedLibc` to `UnsupportedLibcError`). Keyword arguments are now enforced in core API calls. `InotifyConstants.IN_CLOSE` was also removed.
fix
Update class/exception names and ensure keyword arguments are used for API calls.
affects: >=5.0.0
breakingIn v6.0.0, the `inotify` backend now uses `select.poll()` instead of the deprecated `select.select()`, if available. Additionally, several unused functions from the `watchdog.utils.echo` module were removed.
fix
Update any custom `inotify` implementations to reflect `select.poll()` usage if directly interacting with low-level components. Remove any calls to the deprecated `echo` module functions.
affects: >=6.0.0
gotchaText editors like Vim often write to temporary files and then swap them in to replace the original, which may not trigger `on_modified` events.
fix
Configure your editor to disable features that involve backup files or temporary swaps, or consider monitoring the parent directory for `on_moved` events if file replacement is the expected pattern.
affects: All
gotchaOn BSD/macOS systems using `kqueue`, Watchdog opens file descriptors for monitored items. Hitting the operating system's maximum open file descriptor limit can prevent Watchdog from monitoring new files or directories.
fix
Increase the per-process file descriptor limit on your operating system (`ulimit -n` on Unix-like systems) or adjust your monitoring strategy to cover fewer paths if possible.
affects: All (BSD/macOS specific)
gotchaOn Windows, the `ReadDirectoryChangesW` API has limitations: rename/movement events for directories may be reported before I/O is complete, and delete events for directories might be reported as file deletion events.
fix
Implement robust error handling and potentially introduce small delays when processing Windows-specific move/delete events to account for eventual consistency, and be prepared to infer directory deletions from file deletion events.
affects: All (Windows specific)
breakingWatchdog will raise a `FileNotFoundError` if the path being monitored does not exist when the observer is started (`observer.start()`). Ensure all paths passed to `observer.schedule()` are valid and accessible.
fix
Ensure that the directory or file path passed to `observer.schedule()` exists before starting the observer. Verify permissions if the path exists but is inaccessible, as this can also lead to similar errors.
affects: All
gotchaWatchdog raises `FileNotFoundError: [Errno 2] No such file or directory` if the path specified for monitoring (either through `observer.schedule()` or directly to an `Inotify` backend) does not exist on the filesystem at the time monitoring starts. This is a common error indicating an invalid or non-existent path.
fix
Verify that the directory or file path you are attempting to monitor exists and is accessible before calling `observer.start()` or scheduling the watch. This error often suggests a typo in the path argument or that the target filesystem item was deleted unexpectedly.
affects: All
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'watchdog'
The `watchdog` library has not been installed in your Python environment.
fix
pip install watchdog
ImportError: cannot import name 'Observer' from 'watchdog'
The `Observer` class is located within the `watchdog.observers` submodule, not directly under the top-level `watchdog` package.
fix
from watchdog.observers import Observer
OSError: [Errno 28] No space left on device
On Linux, this misleading error often indicates that the system's `inotify` watch limit (the maximum number of files/directories that can be monitored) has been reached.
fix
Increase the `fs.inotify.max_user_watches` kernel parameter, e.g., by running `echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf; sudo sysctl -p`
RuntimeError: Observer is not running.
This occurs when you attempt to call `stop()` or `join()` on an `Observer` instance that has either not been started yet or has already been stopped.
fix
Ensure `observer.start()` is called before `observer.stop()` and `observer.join()`, and avoid calling `stop()` or `join()` multiple times on the same observer instance.
Upgrade
Version history
6.0.0latest on PyPI · released Nov 1, 2024
Audit
Dependencies
PythonrequiredRequires Python 3.9 or above.
PyYAMLoptionalOptional dependency for the 'watchmedo' shell utility.
XCodeoptionalRequired on macOS when installing from sources.
Agent activity
60 hits · last 30 days
node
56
Amazon
1
OpenAI (training)
1
Resources
watchdog — pip install watchdog · libregistry