I use iPython mostly via notebooks but also in the terminal. I just created my default profile by running ipython profile create
.
I can't seem to figure out how to have the profile run several magic commands that I use every time. I tried to look this up online and in a book I'm reading but can't get it to work. For example, if I want %debug
activated for every new notebook I tried adding these lines to my config file:
c.InteractiveShellApp.extensions = ['debug']
or
c.TerminalPythonApp.extensions = ['debug']
and I either get import errors or nothing. My (closely related) questions are the following:
What line to do I add to my ipython config file to activate magic commands? Some require parameters, e.g. %reload_ext autoreload
and %autoreload 2
. How do I also pass these parameters in the config file?
Can I separate which get added for terminal vs. notebooks in a single config file or must I set up separate profiles if I want different magic's activated? (e.g., matplotlib
inline or not). Do the two lines above affect notebooks vs. terminal settings (i.e., c.InteractiveShellApp
vs. c.TerminalPythonApp
)?
Thank you!
IPython stores its files—config, command history and extensions—in the directory ~/. ipython/ by default. If set, this environment variable should be the path to a directory, which IPython will use for user data.
As a first example, you can use the magic command %lsmagic to list the available magic commands (Figure 3.11). To get the output you have to execute the cell as with any other code cell. The %load_ext magic command can be used for loading IPython extension which can add new magic commands.
Capturing Output With %%capture IPython has a cell magic, %%capture , which captures the stdout/stderr of a cell. With this magic you can discard these streams or store them in a variable.
To create a jupyter_notebook_config.py file in the . jupyter directory, with all the defaults commented out, use the following command: $ jupyter notebook --generate-config :ref:`Command line arguments for configuration <config>` settings are documented in the configuration file and the user documentation.
IPython - Magic Commands. Magic commands or magic functions are one of the important enhancements that IPython offers compared to the standard Python shell. These magic commands are intended to solve common problems in data analysis using Python. In fact, they control the behaviour of IPython itself.
You can also create magics of all three kinds by inheriting from the IPython.core.magic.Magics class. This lets you create magics that can potentially hold state in between calls, and that have full access to the main IPython object:
Run the conda package manager within the current kernel. This magic exposes most of the IPython config system. Any Configurable class should be able to be configured with the simple line: Where value will be resolved in the user’s namespace, if it is an expression or variable name.
This magic replaces IPython’s threaded shells that were activated using the (pylab/wthread/etc.) command line flags. GUI toolkits can now be enabled at runtime and keyboard interrupts should work without any problems.
Execute magics as follows:
get_ipython().magic(u"%reload_ext autoreload")
get_ipython().magic(u"%autoreload 2")
You can put those lines in your startup script here:
~/.ipython/profile_default/startup/00-first.py
To start for example the %pylab magic command on startup do the following:
ipython profile create pylab
Add the following code to your .ipython\profile_pylab\ipython_config.py
c.InteractiveShellApp.exec_lines = ['%pylab']
and start ipython
ipython --profile=pylab
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