PyStow

0.8.3 · active · verified Thu Apr 16

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

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `pystow.join` to get and create application-specific directories, and `pystow.ensure` to download and store a file, ensuring its presence. It also shows the `pystow.module` pattern. A temporary custom `PYSTOW_HOME` is set for isolated testing, which defaults to `$HOME/.data` if not configured.

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']

view raw JSON →