Skip to content

environment

Utility module for modifying os.environ

Authors:

  • Toon Willems (Ghent University)
  • Ward Poelmans (Ghent University)

get_changes()

Return tracked changes made in environment.

modify_env(old, new, verbose=True)

Compares two os.environ dumps. Adapts final environment.

read_environment(env_vars, strict=False)

Read variables from the environment

PARAMETER DESCRIPTION
env_vars

a dict with key a name, value a environment variable name

strict

boolean, if True enforces that all specified environment variables are found

DEFAULT: False

reset_changes()

Reset the changes tracked by this module

restore_env(env)

Restore active environment based on specified dictionary.

restore_env_vars(env_keys)

Restore the environment by setting the keys in the env_keys dict again with their old value

sanitize_env()

Sanitize environment.

This function:

  • Filters out empty entries from environment variables like $PATH, $LD_LIBRARY_PATH, etc. Empty entries make no sense, and can cause problems, see for example https://github.com/easybuilders/easybuild-easyconfigs/issues/9843 .

  • Undefines all $PYTHON* environment variables, since they may affect the build/install procedure of Python packages.

cfr. https://docs.python.org/2/using/cmdline.html#environment-variables

While the $PYTHON* environment variables may be relevant/required for EasyBuild itself, and for any non-stdlib Python packages it uses, they are irrelevant (and potentially harmful) when installing Python packages.

Note that this is not an airtight protection against the Python being used in the build/install procedure picking up non-stdlib Python packages (e.g., setuptools, vsc-base, ...), thanks to the magic of .pth files, cfr. https://docs.python.org/2/library/site.html .

setvar(key, value, verbose=True)

put key in the environment with value tracks added keys until write_changes has been called

PARAMETER DESCRIPTION
verbose

include message in dry run output for defining this environment variable

DEFAULT: True

unset_env_vars(keys, verbose=True)

Unset the keys given in the environment Returns a dict with the old values of the unset keys

write_changes(filename)

Write current changes to filename and reset environment afterwards