title: Altering PYTHONPATH at runtime and using a subprocess with a new PYTHONPATH
body: <p>I would like to alter my <code>PYTHONPATH</code> at runtime (using Python 2), put a new <code>PYTHONPATH</code>, execute a <code>subprocess</code> and run my specific code which requires Python3, and then set it back to what it was. </p>
  
  <p>Here is the logic I came up with:</p>
  
  <pre><code># save the current PYTHONPATH to a variable
  pythonpath = os.environ["PYTHONPATH"]
  print('PYTHONPATH (Before Execution): ', pythonpath)
  
  # alter the PYTHONPATH
  # append or insert would not be the best
  # but rather emptying it and putting the new `PYTHONPATH` each time 
  
  # use the following
  # subp.Popen("")
  # subp.call("") : python3 mycode.py -i receivedImgPath", shell=True
  # to call the code with the new PYTHONPATH
  
  
  # after the code execution is done, set the PYTHONPATH to back
  # since we have saved it, use directly the variable pythonpath
  </code></pre>
  
  <p>Is there someone who has done something similar? Is my logic correct? </p>
  
  <p>P.S: I know there are threads like <a href="https://stackoverflow.com/questions/3108285/in-python-script-how-do-i-set-pythonpath">In Python script, how do I set PYTHONPATH?</a>  but they only give information about appending or inserting, which does not work with my logic.</p>