#!/usr/bin/env python # coding: utf-8 # # Sudo in a notebook # # Sudo prompts for a password, # but IPython doesn't notice that this is happening, # and you will get a hang while sudo waits. # # One way to deal with this is to read your password once, # and pass that to sudo with: # # echo PASSWORD | sudo -S ... # # **BE CAREFUL** with this approach, as the password is a global variable, # so may be displayed when using things like `%whos` or `locals()`. # In[1]: from getpass import getpass PASS = getpass('sudo password: ') # In[8]: get_ipython().system('whoami') # In[9]: get_ipython().system("echo $PASS | sudo -S sh -c 'whoami'")