Core foundational package providing base application classes, path utilities, and configuration management for the Jupyter ecosystem.
—
Cross-platform directory discovery and path utilities for managing Jupyter configuration, data, and runtime directories. Provides standardized access to user-level, environment-level, and system-wide Jupyter installations with full support for virtual environments and platform-specific conventions.
Primary functions for discovering standard Jupyter directories across different platforms and installation contexts.
def jupyter_config_dir() -> str:
"""Get the Jupyter config directory for this platform and user.
Returns JUPYTER_CONFIG_DIR if defined, otherwise the appropriate
directory for the platform.
Returns:
str: Path to Jupyter config directory
"""
def jupyter_data_dir() -> str:
"""Get the config directory for Jupyter data files for this platform and user.
These are non-transient, non-configuration files.
Returns JUPYTER_DATA_DIR if defined, else a platform-appropriate path.
Returns:
str: Path to Jupyter data directory
"""
def jupyter_runtime_dir() -> str:
"""Return the runtime dir for transient jupyter files.
Returns JUPYTER_RUNTIME_DIR if defined.
The default is now (data_dir)/runtime on all platforms.
Returns:
str: Path to Jupyter runtime directory
"""Usage Example:
from jupyter_core.paths import (
jupyter_config_dir, jupyter_data_dir, jupyter_runtime_dir
)
# Get standard Jupyter directories
config_dir = jupyter_config_dir() # ~/.jupyter
data_dir = jupyter_data_dir() # ~/.local/share/jupyter
runtime_dir = jupyter_runtime_dir() # ~/.local/share/jupyter/runtime
print(f"Config: {config_dir}")
print(f"Data: {data_dir}")
print(f"Runtime: {runtime_dir}")Functions for discovering search paths that include user, environment, and system-wide locations for Jupyter files and configuration.
def jupyter_path(*subdirs: str) -> list[str]:
"""Return a list of directories to search for data files.
There are four sources of paths to search:
- $JUPYTER_PATH environment variable (always highest priority)
- user directories (e.g. ~/.local/share/jupyter)
- environment directories (e.g. {sys.prefix}/share/jupyter)
- system-wide paths (e.g. /usr/local/share/jupyter)
Args:
*subdirs: Optional subdirectory path components to append
Returns:
list[str]: Ordered list of directories to search
"""
def jupyter_config_path() -> list[str]:
"""Return the search path for Jupyter config files as a list.
Searches user, environment, and system config directories in priority order.
Returns:
list[str]: Ordered list of config directories to search
"""Usage Example:
from jupyter_core.paths import jupyter_path, jupyter_config_path
# Get search paths for data files
data_paths = jupyter_path()
print("Data search paths:", data_paths)
# Get search paths for kernel specifications
kernel_paths = jupyter_path("kernels")
print("Kernel search paths:", kernel_paths)
# Get search paths for config files
config_paths = jupyter_config_path()
print("Config search paths:", config_paths)Functions for determining path behavior based on environment variables and system configuration.
def use_platform_dirs() -> bool:
"""Determine if platformdirs should be used for system-specific paths.
Returns the value of JUPYTER_PLATFORM_DIRS environment variable.
Returns:
bool: True if platformdirs should be used
"""
def prefer_environment_over_user() -> bool:
"""Determine if environment-level paths should take precedence over user-level paths.
Returns True if:
- JUPYTER_PREFER_ENV_PATH is set to a truthy value, OR
- We detect a Python virtual environment and the user owns sys.prefix, OR
- We detect a conda environment (not base) and the user owns sys.prefix
Returns:
bool: True if environment paths should be preferred
"""
def envset(name: str, default: bool | None = False) -> bool | None:
"""Return the boolean value of a given environment variable.
An environment variable is considered set if it is assigned to a value
other than 'no', 'n', 'false', 'off', '0', or '0.0' (case insensitive).
Args:
name: Environment variable name
default: Default value if not defined
Returns:
bool | None: Boolean value of environment variable
"""Utilities for file and directory operations with cross-platform compatibility and security considerations.
def get_home_dir() -> str:
"""Get the real path of the home directory
Returns:
str: Resolved home directory path
"""
def exists(path: str) -> bool:
"""Replacement for `os.path.exists` which works for host mapped volumes
on Windows containers
Args:
path: Path to check
Returns:
bool: True if path exists
"""
def is_hidden(abs_path: str, abs_root: str = "") -> bool:
"""Is a file hidden or contained in a hidden directory?
Checks if a path is hidden based on name (starts with '.') or
platform-specific hidden flags.
Args:
abs_path: Absolute path to check
abs_root: Root directory for relative hidden checks
Returns:
bool: True if file/directory is hidden
"""Functions for creating files with appropriate security permissions, especially important for configuration and credential files.
def secure_write(fname: str, binary: bool = False):
"""Context manager that opens a file with restricted permissions (0o0600).
Creates files that are readable/writable only by the owner.
On Windows, uses platform-specific security restrictions.
Args:
fname: Path to file to create
binary: Whether to open in binary mode
Yields:
file: Opened file handle with secure permissions
"""
def win32_restrict_file_to_user(fname: str) -> None:
"""Secure a windows file to read-only access for the user.
Uses Windows security APIs to restrict file access to current user
and administrators only.
Args:
fname: Path to file to secure
"""
def get_file_mode(fname: str) -> int:
"""Retrieves the file mode in a filesystem-tolerant manner.
Some filesystems auto-enable execute bits, so this function
masks out problematic permission bits for validation.
Args:
fname: Path to file
Returns:
int: Masked file mode suitable for permission checking
"""
def issue_insecure_write_warning() -> None:
"""Issue an insecure write warning when JUPYTER_ALLOW_INSECURE_WRITES is set."""Usage Example:
from jupyter_core.paths import secure_write
# Create a config file with secure permissions
config_data = {"key": "secret_value"}
with secure_write("/path/to/config.json") as f:
import json
json.dump(config_data, f)
# File is created with 0o0600 permissions (user read/write only)Platform-specific constants and paths used throughout the Jupyter ecosystem.
# Application name for directory paths ("Jupyter" on Windows/macOS, "jupyter" on Linux)
APPNAME: str
# System-wide Jupyter data paths
SYSTEM_JUPYTER_PATH: list[str]
# Environment-level Jupyter data paths
ENV_JUPYTER_PATH: list[str]
# System-wide Jupyter config paths
SYSTEM_CONFIG_PATH: list[str]
# Environment-level Jupyter config paths
ENV_CONFIG_PATH: list[str]
# Global flag for allowing insecure writes
allow_insecure_writes: bool
# BSD hidden file flag constant
UF_HIDDEN: int # Value: 32768, used for BSD hidden file detectionThe path management system respects numerous environment variables for customization:
%APPDATA%\jupyter for config by default%APPDATA%\jupyter for data by default~/Library/Jupyter for data directory~/.jupyter for config directory~/.local/share/jupyter for data~/.jupyter for config~/.local/share/jupyter/runtime for runtime filesAutomatic detection and appropriate handling of:
sys.prefix != sys.base_prefixWhen in a virtual environment, environment-level paths take precedence over user-level paths by default, ensuring isolated package installations work correctly.
Install with Tessl CLI
npx tessl i tessl/pypi-jupyter-core