title: Sourcing a file to set environment variables from within a python script
body: <p>Running on Ubuntu, I have a python script, written by someone else, which simply runs like this:</p>
  
  <pre><code>python cool_script.py command line args
  </code></pre>
  
  <p>However, this script needs some environment variables set before it can run. So I created <code>exports.sh</code> located in the same dir as the python script, which looks something like:</p>
  
  <pre><code>export ENV1='foo'
  export ENV2='bar'
  export ENV3='baz'
  </code></pre>
  
  <p>so running:</p>
  
  <pre><code>$source exports.sh
  $python cool_script.py command line args
  </code></pre>
  
  <p>works great. However, my case is a bit more complex. I need to run this on several environments, where the values of ENV1,ENV2,ENV3 will have to be different. I also want to do some stuff to the outputs of <code>cool_script.py</code>. So I wrote <code>cool_script_wrapper.py</code>, which looks like this:</p>
  
  <pre><code># set environment variables
  import os
  exports_file = os.path.dirname(os.path.realpath(__file__)) + '/exports.sh'
  os.system('source %s' % exports_file)
  # run cool_script.py
  os.system('python cool_script.py command line args')
  # do some stuff to the output
  ...
  </code></pre>
  
  <p>I was planning to have different <code>exports.sh</code> scripts for each environment I need to run on, always keeping them in the same dir as the main script. Only problem is that this 'source' approach doesn't work. The environment variables are simply not available to <code>cool_script.py</code>.<br>
  Searching around Stack Overflow, I understand now that it's not supposed to work, but I didn't find any solution that suits my needs. I don't want to use <code>os.environ</code> with hard-coded values, and I don't want to parse the exports file. I guess I could make my wrapper a bash script rather than a python, but I hope there is a better solution. Any ideas?</p>