Skip to content Skip to sidebar Skip to footer

How To Set Environment Variable In Python Script

i am using SCONS Construction tool. i am unable to use the environment variable which is initialized in python script. In My project USER can change some variables to work with th

Solution 1:

SCons by default does not use the invoked environment, this is to make sure that you can reproduce the build no matter which configurations your environment have.

The environment variables are stored within the scons environment under the key ENV so you access the general environment variables like this:

env = Environment()
variable = env['ENV']['SomeVariable']
env['ENV']['SomeVariable'] = SomeValue

I understand your question like you need to use variables set in the python script within SCons. To do this you need to transfer them using the two method you describe in combination.

env = Enviroment()
python_variable = os.environ['SomeVariable']
env['ENV']['SomeVariable'] = python_variable

I would however perhaps recommend other ways of controlling the build, so you do not have to go with the hassle of transferring environment variable. IMHO using arguments are simpler. The arguments are simply a dict that are generated by the invocation of scons, so when you say:

scons -D some_argument=blob

You can get that argument by simply:

some_variable = ARGUMENTS["some_argument"]

Of course I do not know why you need the environment variables, so this might be completely irrelevant for you.

Solution 2:

I once had a similar need, where the compiler was looking for a certain Env variable that hadnt been set. I was able to solve this problem as follows:

env = Environment()
env['ENV']['THE_VARIABLE'] = 'SomeValue'

Post a Comment for "How To Set Environment Variable In Python Script"