what is it?

file watching and code reload in python

installation

pip install watchfiles

usage

watch

paths watched can be directories or files, directories are watched recursively - changes in subdirectories are also detected

from watchfiles import watch
 
for changes in watch('./path/to/dir'):
    print(changes)

awatch

asynchronous equivalent of watch using threads to wait for changes

import asyncio
from watchfiles import awatch
 
async def main():
    async for changes in awatch('/path/to/dir'):
        print(changes)
 
asyncio.run(main())

cli

watchfiles --filter python 'celery -A django_celery_example worker --loglevel=info'
  • --filter python tells watchfiles to only watch py files.
  • By default, watchfiles will watch the current directory and all subdirectories