title: How to set a thread specific environment variable in Python?
body: <p>I want to create two (or more) threads and in each of them to execute a different external program, let's say <code>aaa</code> and <code>bbb</code>. These external programs need libraries located in different directories, let's say in <code>/aaalib</code> and <code>/bbblib</code>, so I have to set the environment variable <code>LD_LIBRARY_PATH</code> before executing them. I want to avoid using shell, so solutions like execution <code>LD_LIBRARY_PATH=/aaalib aaa</code> ain't good.</p>
  
  <p>The question is: how do I set the <code>os.environ['LD_LIBRARY_PATH']</code> in such a way that it will have different values in different threads?</p>
  
  <p>PS. I tried the solution <code>os.environ['...'] = threading.local()</code> described <a href="https://stackoverflow.com/questions/21250645/what-is-a-safe-way-to-bound-environment-variable-in-python">here</a>, but it fails in following way:</p>
  
  <pre><code>In [1]: import os, threading
  In [2]: os.environ['LD_LIBRARY_PATH'] = threading.local()
  -----------------------
  TypeError           Traceback (most recent call last)
  &lt;ipython-input-2-a2c8ef0b901b&gt; in &lt;module&gt;()
  ----&gt; 1 os.environ['LD_LIBRARY_PATH'] = threading.local()
  /usr/lib/python2.7/os.pyc in __setitem__(self, key, item)
  471                 self.data = environ
  472             def __setitem__(self, key, item):
  --&gt; 473                 putenv(key, item)
  474                 self.data[key] = item
  475             def update(self,  dict=None, **kwargs):
  TypeError: must be string, not thread._local
  </code></pre>