iPython - set up magic commands in configuration f

2019-04-24 23:11发布

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:

  1. 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?

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

2条回答
在下西门庆
2楼-- · 2019-04-24 23:20

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
查看更多
虎瘦雄心在
3楼-- · 2019-04-24 23:33

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
查看更多
登录 后发表回答