PyStow
PyStow is a Python library that simplifies the process of picking a consistent and predictable location for storing data generated or consumed by your Python code. It helps manage application data directories and ensures their existence, often defaulting to a structured `$HOME/.data/<app_name>` path. The current version is 0.8.3, and it maintains an active development and release cadence, with version 0.8.4-dev also documented.
Common errors
-
Skipping analyzing "pystow": module is installed, but missing library stubs or py.typed marker
cause PyStow does not currently include a `py.typed` file in its distribution, which is necessary for static type checkers like `mypy` to properly analyze the library's types.fixAdd `ignore_missing_imports = True` for `pystow` in your `mypy` configuration (e.g., in `mypy.ini` under `[mypy-pystow*]`) to suppress this error and allow `mypy` to continue checking the rest of your project. -
FileNotFoundError: [Errno 2] No such file or directory: '/some/unexpected/path/my_app/data.txt'
cause The application is trying to access a file or directory managed by PyStow, but PyStow's base directory or a specific subdirectory was not created, or an environment variable (like `PYSTOW_HOME`) is pointing to a non-existent or inaccessible location.fixEnsure that `pystow.join()` or `pystow.module()` calls are properly made, which automatically create directories. Verify that the `PYSTOW_HOME` environment variable (if set) points to a valid and writable location. Check application logs for any `pystow`-related path resolution issues.
Warnings
- gotcha PyStow defaults to storing data in `$HOME/.data/`. This can be overridden globally by setting the `PYSTOW_HOME` environment variable, or by enabling XDG compatibility with `PYSTOW_USE_APPDIRS=True`, which uses `appdirs` or `platformdirs`.
- gotcha As of version 0.8.3, PyStow does not ship with a `py.typed` file, leading to `mypy` and other static type checkers skipping its analysis and reporting 'missing library stubs or py.typed marker' errors.
- gotcha When using `pystow.ensure`, especially with `name` argument, be aware that the `name` overrides the filename derived from the URL. If the URL does not have a proper filename, `name` is essential, but if it does, `name` changes it. Ensure the `name` matches your expectation for the downloaded file.
Install
-
pip install pystow
Imports
- pystow
import pystow
Quickstart
import pystow
import os
# Configure a custom base directory for PyStow (optional, for testing/isolation)
# Defaults to ~/.data if not set
os.environ['PYSTOW_HOME'] = os.path.join(os.getcwd(), 'my_pystow_data')
# Get a directory for your application data
app_dir = pystow.join('my_app')
print(f"Application data directory: {app_dir}")
app_dir.mkdir(parents=True, exist_ok=True)
# Ensure a file is downloaded from a URL into a specific subdirectory
url = 'https://raw.githubusercontent.com/pykeen/pykeen/master/src/pykeen/datasets/nations/test.txt'
file_path = pystow.ensure(
'my_app', 'datasets', 'nations',
url=url,
name='nations_test.txt'
)
print(f"Ensured file path: {file_path}")
# You can also use modules for cleaner access
my_app_module = pystow.module('my_app')
module_file_path = my_app_module.ensure(
'another_data',
url='https://example.com/some_file.txt',
name='some_file.txt' # This URL is a placeholder and will likely fail.
)
print(f"Ensured file via module: {module_file_path}")
# Clean up the custom directory for demonstration
# import shutil
# if os.path.exists(os.environ['PYSTOW_HOME']):
# shutil.rmtree(os.environ['PYSTOW_HOME'])
# del os.environ['PYSTOW_HOME']