Registry / devops / envsubst

envsubst

JSON →
library0.1.5pypypi✓ verified 86d ago

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 envsubst
INSTALL
IMPORT
SIG · ENVSUBST
E
envsubst
devopspythonv0.1.5
Install
1.5s avg
Import
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.1.5 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.910 runs
installs and imports cleanly · install 0.0s · import 0.000s · 17.8MB
glibc
py 3.103.910 runs
installs and imports cleanly · install 1.5s · import 0.000s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

envsubst
from envsubst import envsubst

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.

import os from envsubst import envsubst # Example 1: Basic substitution os.environ['MY_APP_NAME'] = 'MyAwesomeApp' template_string = "The application name is: ${MY_APP_NAME}." result = envsubst(template_string) print(f"Basic substitution: {result}") # Expected: "The application name is: MyAwesomeApp." # Example 2: Substitution with default value (if variable is unset or empty) # Clear the variable to demonstrate default if 'FEATURE_FLAG' in os.environ: del os.environ['FEATURE_FLAG'] template_with_default = "Feature status: ${FEATURE_FLAG:-disabled}." result_default = envsubst(template_with_default) print(f"With default (unset): {result_default}") # Expected: "Feature status: disabled." os.environ['FEATURE_FLAG'] = 'enabled' result_default_set = envsubst(template_with_default) print(f"With default (set): {result_default_set}") # Expected: "Feature status: enabled." # Clean up environment variables used (good practice for scripts) del os.environ['MY_APP_NAME'] if 'FEATURE_FLAG' in os.environ: del os.environ['FEATURE_FLAG']
envsubst --version
Debug
Known issues
gotchaWhen a referenced environment variable (e.g., `$VAR` or `${VAR}`) is not set or is an empty string, `envsubst` will replace it with an empty string by default, potentially leading to unintended blank values in your output if a default is not specified.
fix
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}')`.
affects: 0.1.0-0.1.5
deprecatedThe `envsubst` library has not seen updates since October 2019. While functional for its stated purpose, it might lack modern features or bug fixes present in more actively maintained alternatives like `varsubst` or `envsub`.
fix
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).
affects: 0.1.0-0.1.5
gotchaThe Python `envsubst` library processes strings in memory. It does not provide direct 'in-place' file modification like the shell command can sometimes appear to (with caveats). Attempting `envsubst < file.txt > file.txt` in shell will truncate the file.
fix
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.
affects: 0.1.0-0.1.5
Errors
Common errors & fixes
KeyError: 'ENV_VAR_NAME'
Attempting to access an environment variable using `os.environ['ENV_VAR_NAME']` directly, but the variable is not set in the current environment.
fix
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}`).
Command 'envsubst' not found
This error typically indicates that the GNU `envsubst` command-line utility is not installed or not in the system's PATH, rather than an issue with the Python `envsubst` library. It's common on macOS without `gettext` installed.
fix
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).
Upgrade
Version history
0.1.5latest on PyPI · released Oct 5, 2019
Audit
Dependencies

No dependency data recorded yet.

Agent activity
4 hits · last 30 days
node
4
Resources
envsubst — pip install envsubst · libregistry