26

I'm trying to make a tutorial for a platform inside a jupyter notebook

at some point I need to run a linux command inside a cell like this :

!sudo apt-get install blah

but cant figure out how to enter the sudo pass , and I dont want to run jupyter notebook with sudo, any idea how to do this ?

Javad Sameri
  • 1,218
  • 3
  • 17
  • 30

5 Answers5

31

Update: I checked all the methods, all of them are working.


1:

Request password using getpass module which essentially hides input by user and then run sudo command in python.

 import getpass
 import os

 password = getpass.getpass()
 command = "sudo -S apt-get update" #can be any command but don't forget -S as it enables input from stdin
 os.system('echo %s | %s' % (password, command))

2:

 import getpass
 import os

 password = getpass.getpass()
 command = "sudo -S apt-get update" # can be any command but don't forget -S as it enables input from stdin
 os.popen(command, 'w').write(password+'\n') # newline char is important otherwise prompt will wait for you to manually perform newline

NOTE for above methods:

The field where you enter the password may not appear in the ipython notebook. It appears in the terminal window on a mac, and I imagine it will appear in a command shell on a PC. Even the result details would appear in the terminal.

3:

You can store your password in mypasswordfile file and just type in cell :

!sudo -S apt-get install blah < /pathto/mypasswordfile # again -S is important here

I would prefer this method if I want to view output of the command in jupyter notebook itself.

References:

  1. Requesting password in IPython notebook

  2. https://docs.python.org/3.1/library/getpass.html

  3. Using sudo with Python script
รยקคгรђשค
  • 1,919
  • 1
  • 10
  • 18
  • 1
    Is there a variation on the above which doesn't leave the password accessible if you step away from the notebook? – Ian Danforth Nov 13 '18 at 22:52
7

I want to point another possibility for the case where jupyter is running on localhost: instead of sudo, use pkexec (or for older systems gksu):

!pkexec apt-get install blah

This will ask for the password in a gui solving the problem...

ntg
  • 12,950
  • 7
  • 74
  • 95
5

You can pass python variables from a notebook to the shell without importing the os or subprocess modules by using the {varname} syntax (e.g. this cool blog).

If you have defined a password and command variable in python (see Suparshva's answer) then you can run this one-liner:

!echo {password}|sudo -S {command}

The exclamation mark tells jupyter to run it in the shell, the echo command will then get the real password (e.g. 'funkymonkey') from the variable called password and then pipe it into the sudo'd command variable (which is a string that describes a shell command, e.g. 'apt-get update').

Sam Murphy
  • 815
  • 11
  • 23
2

You can

subprocess.Pope(['sudo', 'apt-get', 'install', 'bla'])

If you want to avoid the python syntax, you can define your own cell magic that does that for you (e.g. %sudo apt-get install bla).

user2722968
  • 13,636
  • 2
  • 46
  • 67
2

If you don't want your password to be stored somewhere, you could write:

from getpass import getpass
!echo {getpass()} | sudo -S {command}

P.S. this also answers Ian Danforth's question

Christoph
  • 21
  • 1