# qibuild.envsetter – controlling environment variables¶

## qibuild.envsetter.EnvSetter¶

class qibuild.envsetter.EnvSetter

A class to manager environment variables

Typical usage:

envsetter = EnvSetter()
envsetter.prepend_to_path(r"c:\path\to\cmake")
envsetter.set_env_var("WITH_SPAM", "ON")
envsetter.source_bat(r"C:\path\to\vcvars.bat")
build_env = envsetter.get_build_env()
# build_env is now a *copy* of os.environ, os.environ does
# NOT change
qibuild.command.call("cmake", env=build_env)


Notes:

• this will work even in cmake was not in %PATH% before, because we will use build_env when searching for ‘cmake’. (see qibuild.command.call() documentation)
• the source_bat() function is useful to use cl.exe and NMake Makefiles. In this case, you have to source vsvarsall.bat.
get_build_env()

Returns a dict containing the new environnment (note that os.environ is presevered)

prepend_directory_to_variable(directory, variable)

Append a new directory to an environment variable containing a list of paths (most of the time PATH, but can be LIBDIR, for instance when using cl.exe)

• The path will always be sanitize first

(absolute, native path)

• No directory will be added twice

• The variable will be put in upper case on the dictionnary on windows.

prepend_to_path(directory)

Append a directory to PATH environment variable