title: Switch user before executing a python script
body: <p>I am trying to use <code>su</code> linux command in a python script along with <code>getpass</code> and <code>subprocess</code> python standard libraries to switch to a new user before executing my script. I am using this function:</p>
  
  <pre><code>import subprocess
  import getpass
  
  def ask_passwd():
      pw = getpass.getpass(prompt='Please insert the passwd to use the auto api:') # prompt the user for a passwd
      print pw
      command = "su new_user"
      p = subprocess.Popen(command.split(), stdin=subprocess.PIPE)
      p.communicate(pw) # communicate the passwd
  
  def main():
      # code here    
  
  if __name__ == '__main__':
      ask_passwd()
      main()
  </code></pre>
  
  <p>When executing the script the main code works but not the <code>su</code> command. Here what I get back:</p>
  
  <pre><code>Please insert the passwd to use the auto api:
  pass
  su: must be run from a terminal
  </code></pre>
  
  <p>Any help? thanks</p>