我正试图为jupyter笔记本中的平台制作一个教程
在某个时刻,我需要在一个单元格中运行一个linux命令,如下所示:
!sudo apt-get install blah
但是我不知道怎么进入sudo通行证,我也不想和sudo一起运行jupyter笔记本,知道怎么做吗?
我正试图为jupyter笔记本中的平台制作一个教程
在某个时刻,我需要在一个单元格中运行一个linux命令,如下所示:
!sudo apt-get install blah
但是我不知道怎么进入sudo通行证,我也不想和sudo一起运行jupyter笔记本,知道怎么做吗?
我判断了所有的方法,所有的方法都有效.
1:
Request password使用getpass module
,它基本上隐藏了用户的输入,然后运行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:
输入密码的字段可能不会出现在ipython中
3:
您可以将密码存储在mypasswordfile
文件中,只需键入单元格:
!sudo -S apt-get install blah < /pathto/mypasswordfile # again -S is important here
如果我想在jupyter笔记本中查看命令的输出,我更喜欢这种方法.
References: