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?
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.
JupyterLab uses the exact same Notebook server and file format as the classic Jupyter Notebook, so that it is fully compatible with the existing notebooks and kernels. The Classic Notebook and Jupyterlab can run side to side on the same computer. One can easily switch between the two interfaces.
In Jupyter notebooks, the exclamation mark ! executes commands from the underlying operating system. For example, to run the list directory command ls in your Jupyter notebook, call !
There are three types of cells: code cells, markdown cells, and raw cells. Every cell starts off being a code cell, but its type can be changed by using a drop-down on the toolbar (which will be “Code”, initially), or via keyboard shortcuts.
!
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 immediately terminates.
%cd foo
changes the current directory of the notebook process, which is a lasting effect.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With