title: Using Python to open a shell environment, run a command and exit environment
body: <p>I'm trying to automate a process using python. If I am just in the terminal the workflow looks like: </p>
  
  <pre><code>user:&gt; . /path/to/env1.sh
  user:&gt; python something.py
  user:&gt; exit
  user:&gt; . /path/to/env2.sh
  user:&gt; python something2.py
  user:&gt; exit 
  </code></pre>
  
  <p>etc for a few more steps. Each <code>env.sh</code> spawns a new script with a whole slew of environment variables and whatnot set within the current directory. I'm pretty sure I need to use subprocess, but I'm not exactly sure how to go about it. Ideally the workflow would go: open new shell --> run some commands --> exit shell --> repeat as necessary.</p>
  
  <p>EDIT: It seems some clarification is needed. I understand how to use <code>subprocess.Popen()</code> and <code>subprocess.call()</code> to call things from within the shell that the Python script was called from. This is not what I need. When one calls <code>env.sh</code> it sets a whole ton of environment variables and a few other pertinent things and then drops you into a shell to run commands. It is important to note <code>env.sh</code> does not terminate until one types <code>exit</code> after running desired commands. Using <code>subprocess.call("./env.sh", shell = True)</code> opens the shell and stops there. It is just like entering the command <code>./env.sh</code> except that when one issues the <code>exit</code> command, the rest of the python script. So:</p>
  
  <pre><code>subprocess.call(". /path/to/env.sh", shell = True)
  subprocess.call("python something.py", shell = True)
  </code></pre>
  
  <p>Does NOT do what I need it to do, nor does:</p>
  
  <pre><code>p = subprocess.Popen(". /path/to/env.sh", shell = True)
  subprocess.call("python something.py", shell = True)
  p.kill()
  </code></pre>