Home > Back-end >  Is there an advantage of using `%` over `!` in jupyter when the same command exists for both?
Is there an advantage of using `%` over `!` in jupyter when the same command exists for both?

Time:01-07

For example when I use !pip install ... VSCode suggests me to use %pip install .... Similarly there is a version of mv for both ! and %. Is there an advantage to use one over the other?

CodePudding user response:

The main difference that exclamation point ! should be written in IPython notebook before any shell command you want to run in your notebook. Check Shell Assignment in the docs for more of an explanation.

While Magics are specific to and provided by the IPython kernel. To work properly, Magics must use a syntax element which is not valid in the underlying language. For example, the IPython kernel uses the % syntax element for Magics as % is not a valid unary operator in Python.

In addition to that you can create and register your own Magics with IPython. Also you can find many user defined Magics on PyPI.

So we can say that magic % commands provide more functionality which is defined in IPython kernel than just the standard shell commands.

For more info on magic commands check this official reference.

CodePudding user response:

Yes, %pip will properly resolve the appropriate virtual environment in most edge cases, while !pip will not and you may end up installing packages in incorrect locations.

  •  Tags:  
  • Related