Atomic Writes for Home Assistant
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.
Common errors
-
ModuleNotFoundError: No module named 'atomicwrites_homeassistant'
cause The 'atomicwrites-homeassistant' package is not installed in the Python environment.fixInstall the package using pip: 'pip install atomicwrites-homeassistant'. -
ImportError: cannot import name 'atomic_write' from 'atomicwrites_homeassistant'
cause The module is named 'atomicwrites', not 'atomicwrites_homeassistant'.fixUse the correct import statement: 'from atomicwrites import atomic_write'. -
ModuleNotFoundError: No module named 'sqlalchemy'
cause The 'sqlalchemy' package is not installed, which is a dependency for some components.fixInstall the package using pip: 'pip install sqlalchemy'. -
ERROR: Could not install packages due to an OSError: [Errno 13] Permission denied: 'RECORD'
cause Insufficient permissions to write to the installation directory.fixRun the installation command with elevated privileges: 'sudo pip install homeassistant'. -
ModuleNotFoundError: No module named '_sqlite3'
cause The Python installation lacks the SQLite module, which is required for database operations.fixReinstall Python ensuring that the SQLite development headers are present during compilation.
Warnings
- gotcha 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.
- gotcha 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.
- gotcha 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.
- gotcha 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.
Install
-
pip install atomicwrites-homeassistant
Imports
- atomic_write
from atomicwrites import atomic_write
Quickstart
from atomicwrites import atomic_write
file_path = 'my_atomic_file.txt'
content = 'Hello, atomic world!'
# Perform an atomic write, overwriting if the file exists
with atomic_write(file_path, overwrite=True) as f:
f.write(content)
print(f"Content written to {file_path} atomically.")