tox is a generic virtualenv management and test command line tool
Comprehensive configuration management for tox, supporting file-based configuration (INI/TOML), programmatic configuration, and environment-specific settings. The configuration system provides discovery, validation, override mechanisms, and extensibility through plugins.
The central configuration object that manages all tox settings, environment configurations, and runtime options.
class Config:
"""Main configuration object for tox."""
@classmethod
def make(cls, parsed: Parsed, pos_args: Sequence[str] | None,
source: Source, extra_envs: Iterable[str]) -> Config:
"""
Create a configuration instance.
Args:
parsed: Parsed command line arguments
pos_args: Positional arguments
source: Configuration source (file path or discovery result)
extra_envs: Additional environments from plugins
Returns:
Config: Configured instance
"""
def get_env(self, item: str, package: bool = False,
loaders: Sequence[Loader[Any]] | None = None) -> EnvConfigSet:
"""
Get environment-specific configuration.
Args:
item: Environment name (e.g., 'py311', 'docs')
package: Flag indicating if environment is packaging type
loaders: Loaders to use for this configuration
Returns:
EnvConfigSet: Environment configuration
"""
def pos_args(self, to_path: Path | None) -> tuple[str, ...] | None:
"""
Get positional arguments relative to specified path.
Args:
to_path: Base path for relative arguments
Returns:
tuple[str, ...] | None: Positional arguments or None
"""
@property
def core(self) -> CoreConfigSet:
"""Core tox configuration settings."""
@property
def work_dir(self) -> Path:
"""Working directory for tox operations."""
@property
def src_path(self) -> Path:
"""Path to the tox configuration source."""
@property
def options(self) -> Parsed:
"""Parsed command line options."""
def sections(self) -> Iterator[Section]:
"""Iterate over configuration sections."""
def clear_env(self, name: str) -> None:
"""Clear cached environment configuration."""
memory_seed_loaders: defaultdict[str, list[MemoryLoader]]
"""Memory-based configuration loaders."""Base classes for organizing configuration settings into logical groups.
class ConfigSet:
"""Base configuration set for organizing related settings."""
def add_config(self, keys: str | list[str], desc: str, of_type: type,
default=None, **kwargs) -> None:
"""
Add a configuration option.
Args:
keys: Configuration key(s)
desc: Description of the setting
of_type: Expected type
default: Default value
**kwargs: Additional configuration options
"""
def load_config(self) -> None:
"""Load and validate configuration values."""
class CoreConfigSet(ConfigSet):
"""Core tox configuration settings."""
def add_constant(self, keys: str | list[str], desc: str, value) -> None:
"""
Add a constant configuration value.
Args:
keys: Configuration key(s)
desc: Description
value: Constant value
"""
class EnvConfigSet(ConfigSet):
"""Environment-specific configuration settings."""
@property
def name(self) -> str:
"""Environment name."""Classes responsible for loading configuration from different sources.
class Loader:
"""Base configuration loader."""
def load(self, key: str, of_type: type, default=None) -> Any:
"""
Load configuration value.
Args:
key: Configuration key
of_type: Expected type
default: Default value if not found
Returns:
Any: Configuration value
"""
class IniLoader(Loader):
"""Load configuration from INI files (tox.ini, setup.cfg)."""
class TomlLoader(Loader):
"""Load configuration from TOML files (pyproject.toml)."""
class MemoryLoader(Loader):
"""Load configuration from in-memory settings."""Classes for discovering and managing configuration file sources.
class Source:
"""Configuration source abstraction."""
@property
def path(self) -> Path:
"""Path to configuration source."""
@property
def exists(self) -> bool:
"""Whether source exists."""
def discover_source(path: Path) -> Source:
"""
Discover configuration source starting from path.
Args:
path: Starting path for discovery
Returns:
Source: Discovered configuration source
"""Command line parsing and option management.
class ToxParser:
"""Command line parser for tox."""
def add_argument(self, *args, **kwargs) -> None:
"""Add command line argument."""
class Options:
"""Container for parsed command line options."""
@property
def parsed(self) -> Parsed:
"""Parsed command line arguments."""
@property
def pos_args(self) -> Sequence[str] | None:
"""Positional arguments."""
@property
def source(self) -> Source:
"""Configuration source."""
def get_options(*args: str) -> Options:
"""
Parse command line arguments.
Args:
*args: Command line arguments
Returns:
Options: Parsed options
"""[tox]
envlist = py311,py312,docs
skip_missing_interpreters = true
[testenv]
deps = pytest>=6.0
pytest-cov
commands = pytest {posargs}
passenv = CI
[testenv:docs]
deps = sphinx
sphinx-rtd-theme
commands = sphinx-build -b html docs docs/_build/html[tool.tox]
legacy_tox_ini = """
[tox]
envlist = py311,py312,docs
[testenv]
deps = pytest>=6.0
pytest-cov
commands = pytest {posargs}
"""envlist: List of environments to run by defaultskip_missing_interpreters: Skip missing Python interpreterswork_dir: Working directory for tox operationstemp_dir: Temporary directorypackage_dir: Directory containing the packagemin_version: Minimum required tox versionbasepython: Base Python interpreterdeps: Dependencies to installcommands: Commands to executecommands_pre: Commands to run before main commandscommands_post: Commands to run after main commandsinstall_command: Package installation commandchangedir: Working directory for commandspassenv: Environment variables to pass throughsetenv: Environment variables to setallowlist_externals: Allowed external commandsskip_install: Skip package installationpackage: Package type (wheel, sdist, skip)build_dir: Build directorypackage_env: Environment for package buildingwheel_build_env: Environment for wheel buildingOverride configuration values from command line:
# Override single value
tox --override testenv.deps=pytest>=7.0
# Override multiple values
tox --override testenv.commands="pytest -v" --override tox.skip_missing_interpreters=falseProgrammatic override:
from tox.config.main import Config
# Create config with overrides
config = Config.make(parsed_args, pos_args, source, extended_envs)
# Access overridden values
env_config = config.get_env('py311')
deps = env_config['deps']Tox searches for configuration in this order:
tox.ini (preferred)pyproject.toml with [tool.tox] sectionsetup.cfg with [tox:tox] sectionDiscovery starts from current directory and searches up the directory tree until a configuration file is found.
Plugins can extend configuration through hooks:
from tox.plugin import impl
@impl
def tox_add_core_config(core_conf: CoreConfigSet) -> None:
"""Add core configuration options."""
core_conf.add_config(
keys="custom_option",
desc="Custom plugin option",
of_type=str,
default="default_value"
)
@impl
def tox_add_env_config(env_conf: EnvConfigSet) -> None:
"""Add environment configuration options."""
env_conf.add_config(
keys="env_custom_option",
desc="Custom environment option",
of_type=bool,
default=False
)Install with Tessl CLI
npx tessl i tessl/pypi-tox