Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between ! and % in Jupyter notebooks?

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?

like image 860
zthomas.nc Avatar asked Aug 20 '17 16:08

zthomas.nc


People also ask

What does %% do in 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.

What is the difference between Jupyter and Jupyter Notebook?

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.

What is Jupyter exclamation mark?

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 !

What are the three main types of Jupyter Notebook cell?

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.


1 Answers

! 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.

like image 98
cco Avatar answered Oct 02 '22 07:10

cco