简体   繁体   中英

Python script running from SCONS cannot access environment variables

Inside of my scons script I execute another python script:

fs = env.Command('fs', None, 'python updatefs.py')
AlwaysBuild(fs)
Depends(fs,  main)

In python script I am trying to access an environment variable:

import os
mode = os.environ['PROC_MODE']

The variable was previously set up in the shell:

export PROC_MODE='some_mode'

Python complain:

KeyError: 'PROC_MODE'

What is the proper way to propagate environment to an external script?

This is covered in lightly in the FAQ:

FAQ

Basically SCons constructs a clean reproducible set of environment variables so that differences in any user's environment won't break a build.

So if you want to propagate a particular variable from your shell you can explicitly do it as such:

env['ENV']['MY_VARIABLE']=os.environ['MY_VARIABLE']

If you wanted to progagate all environment variables you'd do this:

env['ENV'] = os.environ

Where env is your Environment()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM