{"id":6525,"library":"atomicwrites-homeassistant","title":"Atomic Writes for Home Assistant","description":"A fork of the `atomicwrites` library, `atomicwrites-homeassistant` provides atomic file writes in Python. It ensures that file writes are all-or-nothing operations, preventing data corruption in case of crashes or interruptions. The `homeassistant` fork specifically addresses maintenance issues of the original `atomicwrites` package and is currently at version 1.4.1. It is a dependency for Home Assistant and other projects requiring reliable file operations.","status":"active","version":"1.4.1","language":"en","source_language":"en","source_url":"https://github.com/untitaker/python-atomicwrites","tags":["file-io","atomic-writes","home-assistant","data-integrity"],"install":[{"cmd":"pip install atomicwrites-homeassistant","lang":"bash","label":"Install via pip"}],"dependencies":[],"imports":[{"symbol":"atomic_write","correct":"from atomicwrites import atomic_write"}],"quickstart":{"code":"from atomicwrites import atomic_write\n\nfile_path = 'my_atomic_file.txt'\ncontent = 'Hello, atomic world!'\n\n# Perform an atomic write, overwriting if the file exists\nwith atomic_write(file_path, overwrite=True) as f:\n    f.write(content)\n\nprint(f\"Content written to {file_path} atomically.\")","lang":"python","description":"This example demonstrates how to use `atomic_write` as a context manager to ensure that file writing is an atomic operation. The `overwrite=True` parameter ensures that if the file already exists, it will be safely replaced."},"warnings":[{"fix":"Ensure you are installing `atomicwrites-homeassistant` when intending to use the Home Assistant-maintained version.","message":"The `atomicwrites-homeassistant` package is a fork created because the original `atomicwrites` library was unmaintained. Users should be aware they are using this Home Assistant-specific fork, which provides continued maintenance.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Do not solely rely on the library for guaranteed atomicity on Windows without additional validation or error handling for critical operations. Consider alternative strategies if absolute atomicity is a must-have.","message":"On Windows, the atomicity guarantee for file moves (using `MoveFileEx` via `ctypes`) is not absolute; it may silently fall back to a non-atomic method in some cases. If strict atomicity is critical on Windows, additional safeguards or alternative methods may be required.","severity":"gotcha","affected_versions":"All versions"},{"fix":"To resolve this, avoid bind-mounting individual configuration files. Instead, bind-mount the parent directory (e.g., `/config`) or use Docker volumes. Alternatively, edit these files directly on the host or accept that UI editing will not work for bind-mounted files.","message":"When running Home Assistant in Docker and using individually bind-mounted configuration files (e.g., for automations or scripts), atomic writes can lead to an `OSError: [Errno 16] Resource busy`. This prevents UI editors from saving changes to these files.","severity":"gotcha","affected_versions":"All versions with Docker bind mounts"},{"fix":"Be aware of potential dual file entries during a brief transition period. If preserving specific permissions is crucial, implement explicit `chmod` calls after the atomic write, considering concurrency implications.","message":"On POSIX systems, there is a small time window during `link` and `unlink` operations where the file might be accessible under both its temporary name and the target name. Additionally, file permissions of the target file may change and are not automatically preserved by the library due to concurrency concerns.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-15T00:00:00.000Z","next_check":"2026-07-14T00:00:00.000Z","problems":[{"fix":"Install the package using pip: 'pip install atomicwrites-homeassistant'.","cause":"The 'atomicwrites-homeassistant' package is not installed in the Python environment.","error":"ModuleNotFoundError: No module named 'atomicwrites_homeassistant'"},{"fix":"Use the correct import statement: 'from atomicwrites import atomic_write'.","cause":"The module is named 'atomicwrites', not 'atomicwrites_homeassistant'.","error":"ImportError: cannot import name 'atomic_write' from 'atomicwrites_homeassistant'"},{"fix":"Install the package using pip: 'pip install sqlalchemy'.","cause":"The 'sqlalchemy' package is not installed, which is a dependency for some components.","error":"ModuleNotFoundError: No module named 'sqlalchemy'"},{"fix":"Run the installation command with elevated privileges: 'sudo pip install homeassistant'.","cause":"Insufficient permissions to write to the installation directory.","error":"ERROR: Could not install packages due to an OSError: [Errno 13] Permission denied: 'RECORD'"},{"fix":"Reinstall Python ensuring that the SQLite development headers are present during compilation.","cause":"The Python installation lacks the SQLite module, which is required for database operations.","error":"ModuleNotFoundError: No module named '_sqlite3'"}]}