之间有什么区别!和Jupyter笔记本中的%?

问题描述:

都允许您从Jupyter笔记本运行Shell命令.

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

  • 提供.IPython内核,并允许您运行魔术命令",其中许多命令都包含众所周知的shell命令.

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

允许Shell命令在单元中运行.

!, provided by Jupyter, allows shell commands to be run within cells.

我一直找不到很多比较两者,以及简单的shell命令,例如 cd 等.我看到的主要区别是是交互式的,实际上会更改您在shell 在笔记本上.

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.

考虑在Jupyter笔记本中用于shell命令的符号时,是否还有其他对比点或规则?

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

调用shell(在新进程中),而影响相关的进程使用笔记本(或笔记本本身;许多命令没有对应的shell).

! 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 本身没有持久作用,因为具有更改目录的进程会立即终止.

!cd foo, by itself, has no lasting effect, since the process with the changed directory immediately terminates.

%cd foo 更改笔记本进程的当前目录,这是持久的作用.

%cd foo changes the current directory of the notebook process, which is a lasting effect.