Difference between ! and % in Jupyter Notebooks

a 夏天 提交于 2020-05-24 20:24:05

问题


Both ! and % allow you to run shell commands from a Jupyter notebook.

% is provided by the IPython kernel and allows you to run "magic commands", many of which include well-known shell commands. !, provided by Jupyter, allows shell commands to be run within cells.

I haven't been able to find much comparing the two, and for simple shell commands like cd, etc. the main difference I see is that % is interactive and will actually change your location in the shell while in the notebook.

Are there other points or rules of contrast when thinking about which symbol to use for shell commands in a Jupyter Notebook?


回答1:


! calls out to a shell (in a new process), while % affects the process associated with the notebook (or the notebook itself; many % commands have no shell counterpart).
!cd foo, by itself, has no lasting effect, since the process with the changed directory immediayely terminates. %cd foo changes the current directory of the notebook process, which is a lasting effect.



来源:https://stackoverflow.com/questions/45784499/difference-between-and-in-jupyter-notebooks

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!