Jupyter Notebook is very convenient as a convenient execution environment for Python, and I love it. You can add languages other than Python, and you can also execute Linux commands (magic commands) on your notebook.
You can execute the ls
command and the cd
command as shown below.
!ls
%cd
The pip command can also be executed as a magic command. If you want to install pandas, you can execute it with the following command. If you want to add additional required libraries while using Jupyter Notebook If you write the following command, you can rest assured when sharing Notebook.
!pip install pandas
The main subject is from here. Try running a command that needs to be executed with administrator privileges (sudo). Let's run ʻapt` here. In the Python library, there is a thing [^ app] that sometimes requires you to install an application with the package manager of the OS separately. If you can execute it on Notebook, you can prevent forgetting to install it.
Try running ! Sudo apt update
as it is.
!sudo apt update
The result is as follows. You cannot enter the password in this state.
Try passing the password as a string using the echo command. Pass the -S
option to the sudo command. You can now pass the password as a command line argument. (Actually, the method of inputting the password from the standard input)
!echo "passwd" | sudo -S apt udate
It worked safely. However, it becomes complicated because it is necessary to rewrite the character string when sharing the notebook. In addition, the password is completely known as it is, which is not preferable for security. If the password is leaked, it does not make sense. .. ..
Write the password in a file called passwd.txt
, and change the method to read and pass the contents of that file. This way you don't have to worry about your password being accidentally leaked. Use the xargs
command to read the file. A command that creates command line arguments. Here, the file is specified by passing the -a
option (--arg-file =).
!xargs -a passwd.txt | sudo apt update
I was able to execute it safely. There is no need to worry about password leakage, and there is no need to rewrite the password part when sharing a notebook, eliminating complicated work [^ passwd]. This is safe.
Reference
[^ app]: Mecab and GraphViz need to be installed separately from the Python version.
[^ passwd]: It is necessary to prepare a file equivalent to passwd.txt. .. ..
Recommended Posts