ImportingEnvironmentSettings - SCons/scons GitHub Wiki

SCons attempts to be a clean build tool and only generates the environment for build operations that is necessary for success of those build operations. This is the philosophical other end of the scale from make, where all of the environment variables of the process are automatically active within the build statements.

This is a good thing, since it prevents hidden dependencies and other problems (even trojan horses) from affecting the build environment from one user or platform to another.

However, sometimes you do want to import settings from your environment (see the ExternalTools example).

/!\ First, you should think about importing ONLY those settings you want:

env = Environment()
env.Append(CCACHEDIR=os.environ['CCACHEDIR'])

If this is too awkward, /!\ think again, and again!

Do you really want to open this Pandora's box?!

If you still do, then you could use the simple method shown in ExternalTools:

env = Environment(ENV=os.environ)

but this is likely to break for some platforms (e.g. Win32) because the Platform initialization of SCons may have already prepared some settings (like paths, etc.) that would be overwritten and lost by the above substitution. The following method is a little more involved, but makes sure that combinative elements like paths are correctly concatenated rather than replaced:

def ENV_update(tgt_ENV, src_ENV):
    for K in src_ENV.keys():
        if K in tgt_ENV.keys() and K in ["PATH", "LD_LIBRARY_PATH", "LIB", "INCLUDE"]:
            tgt_ENV[K] = SCons.Util.AppendPath(tgt_ENV[K], src_ENV[K])
        else:
            tgt_ENV[K] = src_ENV[K]


env = Environment()
ENV_update(env, os.environ)

I tried the above and got the following error

AttributeError: SConsEnvironment instance has no attribute 'keys':
  File "SConstruct", line 14:
    ENV_update(env, os.environ)
  File "SConstruct", line 7:
    if K in tgt_ENV.keys() and K in [ 'PATH', 'LD_LIBRARY_PATH',

I suspect ENV_update is supposed to be called as follows

ENV_update(env['ENV'], os.environ)

SCons 0.96.1; Python 2.4.1; WinXP; - TomHoward


Sorry to ruin this page. I run scons 0.95 on Windows ME with VC6. I alway get the anonying messages of 'Bad command or filename' when invoking cl.exe or link.exe, and my environment is OK. Finally, I change

args = [sh, '/C', escape(string.join(args))]

in Platform\win32.py to

args = [sh, '/E:4096 /C', string.join(args)]

Everything seems right then. That's all.


Some much later updates... Environment instances (and overrides) do now provide a keys() method.

The tricky thing about updating environments is: keyword-style assignments in the Environment() call overwrite, and risk causing problems. For example, assigning to ENV on Windows means the original setting of ENV['SystemRoot'] is clobbered, and there are commands on Windows that simply won't work without an environment setting of SystemRoot. On the other hand, using a method like env.Append avoids that problem, but in some cases comes too late - if the setting is needed by a tool, by the time you have created the Environment instance, the tool initialization has already run, and won't notice that you later changed a setting. Care is needed to get this right!