title: Emulating Bash 'source' in Python
body: <p>I have a script that looks something like this:</p>
  
  <pre><code>export foo=/tmp/foo                                          
  export bar=/tmp/bar
  </code></pre>
  
  <p>Every time I build I run 'source init_env' (where init_env is the above script) to set up some variables.</p>
  
  <p>To accomplish the same in Python I had this code running,</p>
  
  <pre><code>reg = re.compile('export (?P&lt;name&gt;\w+)(\=(?P&lt;value&gt;.+))*')
  for line in open(file):
      m = reg.match(line)
      if m:
          name = m.group('name')
          value = ''
          if m.group('value'):
              value = m.group('value')
          os.putenv(name, value)
  </code></pre>
  
  <p>But then <em>someone</em> decided it would be nice to add a line like the following to the <code>init_env</code> file:</p>
  
  <pre><code>export PATH="/foo/bar:/bar/foo:$PATH"     
  </code></pre>
  
  <p>Obviously my Python script fell apart. I could modify the Python script to handle this line, but then it'll just break later on when <em>someone</em> comes up with a new feature to use in the <code>init_env</code> file.</p>
  
  <p>The question is if there is an easy way to run a Bash command and let it modify my <code>os.environ</code>?</p>