title: Using subprocess to get output
body: <p>Using the subprocess module how do I get the following command to work?</p>
  
  <pre><code>isql -v -b -d, DSN_NAME "DOMAIN\username" password &lt;&lt;&lt; 
  "SELECT column_name, data_type 
   FROM database_name.information_schema.columns 
   WHERE table_name = 'some_table';"
  </code></pre>
  
  <p>This command works perfectly when I run it in a bash shell but I can't get it to work when running from within Python.  I'm trying to do this from within Python because I need to be able to modify the query and get different result sets back and then process them in Python.  I can't use one of the nice Python database connectors for various reasons which leaves me trying to pipe output from isql.</p>
  
  <p>My code currently looks similar to the following:</p>
  
  <pre><code>bash_command = '''
                 isql -v -b -d, DSN_NAME "DOMAIN\username" password &lt;&lt;&lt; 
                 "SELECT column_name, data_type 
                  FROM database_name.information_schema.columns 
                  WHERE table_name = 'some_table';" 
                 '''
  process = subprocess.Popen(bash_command,
                             shell=True,
                             stdout=subprocess.PIPE,
                             stderr=subprocess.PIPE)
  output, error = process.communicate()
  </code></pre>
  
  <p>However I have tried lots of variations: </p>
  
  <ul>
  <li>Using the entire command as a string, or as a list of strings. </li>
  <li>Using check_output vs Popen. </li>
  <li>Using communicate() to try and send the query to the isql command or having the query be part of the command string using a heredoc.  </li>
  <li>Using shell = True or not.  </li>
  <li>Specifying /bin/bash or using the default /bin/sh. </li>
  <li>Lots of different quoting and escaping patterns.</li>
  </ul>
  
  <p>And pretty much every permutation of the above. </p>
  
  <p>In no case do I receive the output of the query that I'm looking for.  I'm pretty sure that the command isn't being sent to the shell as is but I can't tell what is being sent to the shell.</p>
  
  <p>I feel like this should be pretty simple, send a command to the shell and get the output back, but I just can't make it work.  I can't even see what command is being sent to the shell, even using pdb.</p>