Appdata Management Utilities
The `appdata` library provides utilities for managing application-specific data folders across different operating systems (Windows, macOS, Linux). It helps locate standard directories for user data, configuration, and cache, ensuring cross-platform compatibility. The current version is 2.2.1, with releases typically occurring on an as-needed basis to address bugs or introduce minor enhancements.
Common errors
-
ModuleNotFoundError: No module named 'appdata.appdata_paths'
cause Attempting to import the `AppdataPaths` class which was removed in `appdata` version 2.0.0.fixUpdate your import statement to `from appdata import AppData` and refactor your code to use the `AppData` class instead. -
TypeError: can only concatenate str (not "PosixPath") to str
cause This error occurs when you try to concatenate a `pathlib.Path` object (returned by `appdata` v2.0+) with a string using the `+` operator.fixUse the `/` operator provided by `pathlib` for path concatenation (e.g., `my_path / 'filename.txt'`) or convert the `pathlib.Path` object to a string first using `str(my_path)`. -
AttributeError: type object 'AppData' has no attribute 'user_data_dir'
cause You are trying to access instance attributes (like `user_data_dir`) directly on the `AppData` class without creating an instance first.fixFirst, create an instance of `AppData` by calling it with your application name and author (e.g., `app_data = AppData('MyApplication', 'MyCompany')`), then access the attributes on that instance (e.g., `app_data.user_data_dir`).
Warnings
- breaking The `AppdataPaths` class was completely removed in version 2.0.0. Its functionality was merged into the `AppData` class.
- breaking In version 2.0.0, the methods `user_data_dir`, `user_config_dir`, `user_cache_dir`, `site_data_dir`, and `site_config_dir` (and their equivalents) now return `pathlib.Path` objects instead of strings.
- gotcha Attempting to concatenate a `pathlib.Path` object directly with a string using `+` will result in a `TypeError`.
Install
-
pip install appdata
Imports
- AppData
from appdata.appdata_paths import AppdataPaths
from appdata import AppData
Quickstart
import os
from appdata import AppData
# Replace 'MyAwesomeApp' and 'MyCompany' with your actual app/company names
APP_NAME = os.environ.get('APPDATA_APP_NAME', 'MyTestApp')
APP_AUTHOR = os.environ.get('APPDATA_APP_AUTHOR', 'MyTestCompany')
# Initialize AppData for your application
app_data = AppData(APP_NAME, APP_AUTHOR)
# Access common application directories
user_data_dir = app_data.user_data_dir
user_config_dir = app_data.user_config_dir
user_cache_dir = app_data.user_cache_dir
print(f"User Data Directory: {user_data_dir} (Type: {type(user_data_dir)})")
print(f"User Config Directory: {user_config_dir} (Type: {type(user_config_dir)})")
print(f"User Cache Directory: {user_cache_dir} (Type: {type(user_cache_dir)})")
# You can create subdirectories and files easily with pathlib.Path
some_file_path = user_data_dir / "settings.json"
print(f"Example file path using pathlib: {some_file_path}")
# To convert to string for older APIs if needed:
print(f"User data as string: {str(user_data_dir)}")