experimental/sources/: expandvars-0.12.0 metadata and description

Simple index

Expand system variables Unix style

author_email Arijit Basu <sayanarijit@gmail.com>
  • Development Status :: 4 - Beta
  • Intended Audience :: Developers
  • Intended Audience :: Information Technology
  • Intended Audience :: Other Audience
  • License :: OSI Approved :: MIT License
  • Natural Language :: English
  • Operating System :: MacOS
  • Operating System :: Microsoft
  • Operating System :: POSIX
  • Operating System :: Unix
  • Programming Language :: Python :: 3
  • Topic :: Software Development
  • Topic :: Utilities
description_content_type text/markdown
keywords expand,system,variables
license MIT License Copyright (c) 2019 Arijit Basu Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
maintainer_email Arijit Basu <sayanarijit@gmail.com>
  • Homepage, https://github.com/sayanarijit/expandvars
provides_extras tests
  • black; extra == 'tests'
  • pytest; extra == 'tests'
  • pytest-cov; extra == 'tests'
  • tox; extra == 'tests'
requires_python >=3
File Tox results History
11 KB


Expand system variables Unix style

PyPI version codecov


This module is inspired by GNU bash's variable expansion features. It can be used as an alternative to Python's os.path.expandvars function.

A good use case is reading config files with the flexibility of reading values from environment variables using advanced features like returning a default value if some variable is not defined. For example:

my_secret_access_code = "${ACCESS_CODE:-default_access_code}"
my_important_variable = "${IMPORTANT_VARIABLE:?}"
my_updated_path = "$PATH:$HOME/.bin"
my_process_id = "$$"
my_nested_variable = "${!NESTED}"

NOTE: Although this module copies most of the common behaviours of bash, it doesn't follow bash strictly. For example, it doesn't work with arrays.



pip install expandvars


conda install -c conda-forge expandvars


from expandvars import expandvars

# /bin:/sbin:/usr/bin:/usr/sbin:/home/you/bin:/default/path


For now, refer to the test cases to see how it behaves.



If you want to enable strict parsing by default, (similar to set -u / set -o nounset in bash), pass nounset=True.

# All the variables must be defined.
expandvars("$VAR1:${VAR2}:$VAR3", nounset=True)

# Raises UnboundVariable error.

NOTE: Another way is to use the ${VAR?} or ${VAR:?} syntax. See the examples in tests.


If you want to temporarily disable strict parsing both for nounset=True and the ${VAR:?} syntax, set environment variable EXPANDVARS_RECOVER_NULL=somevalue. This helps with certain use cases where you need to temporarily disable strict parsing of critical env vars, e.g. in testing environment, without modifying the code.


EXPANDVARS_RECOVER_NULL=foo myapp --config production.ini && echo "All fine."

WARNING: Try to avoid export EXPANDVARS_RECOVER_NULL because that will disable strict parsing permanently until you log out.


You can customize the variable symbol and data used for the expansion by using the more general expand function.

from expandvars import expand

print(expand("%PATH:$HOME/bin:%{SOME_UNDEFINED_PATH:-/default/path}", environ={"PATH": "/example"}, var_symbol="%"))
# /example:$HOME/bin:/default/path


To contribute, setup environment following way:


# Clone repo
git clone https://github.com/sayanarijit/expandvars && cd expandvars

# Setup virtualenv
python -m venv .venv
source ./.venv/bin/activate

# Install as editable including test dependencies
pip install -e ".[tests]"