Django Watchfiles
django-watchfiles is a Django package that enhances Django's built-in autoreloader by utilizing the more efficient `watchfiles` library. It provides faster and more reliable file change detection during development, leading to quicker server reloads. The current version is 1.4.0. It has an active but infrequent release cadence, typically releasing new versions as `watchfiles` or Django versions evolve.
Common errors
-
ModuleNotFoundError: No module named 'django_watchfiles'
cause The `django-watchfiles` package is not installed in your current Python environment.fixRun `pip install django-watchfiles`. -
Changes to Python files (or other watched files) are not triggering the Django development server to reload.
cause `django_watchfiles` is likely not added to `INSTALLED_APPS`, or `settings.DEBUG` is set to `False`.fixEnsure `'django_watchfiles'` is present in your `INSTALLED_APPS` list and `DEBUG = True` in your `settings.py`. -
ImportError: cannot import name 'FileWatcher' from 'watchfiles'
cause The `watchfiles` dependency is too old for the installed `django-watchfiles` version (e.g., using `django-watchfiles>=1.0` with `watchfiles<0.17.0`).fixUpgrade `watchfiles` to a compatible version: `pip install --upgrade watchfiles`.
Warnings
- breaking django-watchfiles v1.0 and later require watchfiles version 0.17.0 or newer. Older watchfiles versions are incompatible and will lead to errors.
- gotcha django-watchfiles, like Django's default autoreloader, only functions when `settings.DEBUG` is set to `True`. It has no effect in production environments.
- gotcha Files changed outside your project's root directory or an explicitly configured `WATCHFILES_WATCH_DIR` will not trigger reloads. Ensure all relevant files are within the watched paths.
- breaking django-watchfiles requires Django 3.2 or newer. Older Django versions are not supported.
Install
-
pip install django-watchfiles
Imports
- WatchfilesAppConfig
from django_watchfiles.apps import WatchfilesAppConfig
Quickstart
INSTALLED_APPS = [
# ... other apps ...
'django_watchfiles',
]
# Optional: Customize watch directory (default is project root)
# WATCHFILES_WATCH_DIR = '/path/to/your/custom/dir'
# Optional: Ignore specific patterns (default includes common ignores)
# WATCHFILES_IGNORE_PATTERNS = ['__pycache__/', '*.log']