Search code examples
pythonvariablesimportenvironmentscons

scons "import os" is not importing my OS environment variable?


I'm testing the behavior of scons on MAC, I tried "import os" to construct from environment variable, like this:

$cat SConstruct
import os
env=Environment(ENV=os.environ)
kkk=env['MY']

Then I tried to run it, seems still the environment variable is not imported by "env=Environment(ENV=os.environ)" statement

$export MY='haha'
$/usr/local/bin/scons
scons: Reading SConscript files ...
KeyError: 'MY':
  File "/Users/my/SConstruct", line 3:
    kkk=env['MY']
  File "/usr/local/Cellar/scons/2.5.1/libexec/scons-local/SCons/Environment.py", line 410:
    return self._dict[key]

I'm using scons version:

$/usr/local/bin/scons -v
SCons by Steven Knight et al.:
    script: v2.5.1.rel_2.5.1:3735:9dc6cee5c168[MODIFIED], 2016/11/03 14:02:02, by bdbaddog on mongodog
    engine: v2.5.1.rel_2.5.1:3735:9dc6cee5c168[MODIFIED], 2016/11/03 14:02:02, by bdbaddog on mongodog
    engine path: ['/usr/local/Cellar/scons/2.5.1/libexec/scons-local/SCons']
Copyright (c) 2001 - 2016 The SCons Foundation

Where did I get wrong, how to fix it?


Solution

  • Try:

    import os
    env=Environment(ENV=os.environ)
    my_env_value=env['ENV']['MY']
    

    The environment presented to commands run by SCons is env['ENV'], and not env itself.