The `envsubst` library for Python provides functionality to substitute environment variables in a string, mimicking the behavior of the GNU `envsubst` command-line utility. It supports `$VAR` and `${VAR}` style variable expansions, including the use of default values like `${VAR:-default}`. The current version is 0.1.5, with its last release in October 2019, indicating a maintenance-only or inactive development cadence.
pip install envsubstVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to import and use the `envsubst` function to replace environment variables in a string. It covers both basic substitution and using default values when a variable is unset or empty. The example explicitly sets and cleans up environment variables for reproducible results.
Always use default value syntax (`${VAR:-default}`) if the variable might be unset or empty and a fallback is desired. E.g., `envsubst('${DATABASE_URL:-sqlite:///data.db}')`.For new projects or if advanced features (e.g., custom resolvers, Rust performance) are required, consider alternatives such as `varsubst` (pip install varsubst) or `envsub` (pip install envsub).
To modify a file, read its content, process with `envsubst`, and then write the result back to the file (or a new file). For shell scripting, use a temporary file or `sponge` for in-place edits.
When fetching environment variables that might not be set, use `os.environ.get('ENV_VAR_NAME', 'default_value')` to provide a fallback, or ensure the variable is always set before runtime. The `envsubst` function itself will substitute unset variables with empty strings if no default is provided in the template (e.g., `${ENV_VAR_NAME:-default}`).If you intend to use the Python library, ensure you are importing and calling `envsubst` within your Python code (`from envsubst import envsubst`). If you need the shell command, install `gettext` (e.g., `brew install gettext` on macOS, `sudo apt-get install gettext-base` on Debian/Ubuntu).
No dependency data recorded yet.