iPython - 在配置文件中设置魔术命令

iPython - set up magic commands in configuration file

我主要通过笔记本使用 iPython,但也在终端中使用。我刚刚通过 运行ning ipython profile create.

创建了我的默认配置文件

我似乎无法弄清楚如何让配置文件 运行 我每次都使用几个神奇的命令。我试图在网上和我正在阅读的一本书中查找它,但无法正常工作。例如,如果我想为每个新笔记本激活 %debug,我尝试将这些行添加到我的配置文件中:

c.InteractiveShellApp.extensions = ['debug']

c.TerminalPythonApp.extensions = ['debug']

而且我要么遇到导入错误,要么一无所获。我的(密切相关的)问题如下:

  1. 我要在 ipython 配置文件中添加哪一行来激活魔法命令?有些需要参数,例如%reload_ext autoreload%autoreload 2。如何在配置文件中也传递这些参数?

  2. 我可以在单个配置文件中将为终端和笔记本添加的内容分开吗?或者如果我想激活不同的魔法,我必须设置单独的配置文件吗? (例如,matplotlib 是否内联)。上面两行是否会影响笔记本与终端设置(即 c.InteractiveShellAppc.TerminalPythonApp)?

谢谢!

执行如下魔法:

get_ipython().magic(u"%reload_ext autoreload")
get_ipython().magic(u"%autoreload 2")

您可以将这些行放在您的启动脚本中:

~/.ipython/profile_default/startup/00-first.py

例如,要在启动时启动 %pylab 魔术命令,请执行以下操作:

ipython profile create pylab

将以下代码添加到您的 .ipython\profile_pylab\ipython_config.py

c.InteractiveShellApp.exec_lines = ['%pylab']

并开始 ipython

ipython --profile=pylab