问题
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