【问题标题】:iPython - set up magic commands in configuration fileiPython - 在配置文件中设置魔术命令
【发布时间】:2015-09-20 15:43:10
【问题描述】:

我主要通过笔记本使用 iPython,但也在终端中使用。我刚刚通过运行ipython profile create 创建了我的默认配置文件。

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

c.InteractiveShellApp.extensions = ['debug']

c.TerminalPythonApp.extensions = ['debug']

我要么得到导入错误,要么什么都没有。我的(密切相关的)问题如下:

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

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

谢谢!

【问题讨论】:

    标签: python ipython ipython-notebook


    【解决方案1】:

    如下执行魔法:

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

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

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

    【讨论】:

    • 正是我想要的。尤其是autoreload 魔法:D
    【解决方案2】:

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

    ipython profile create pylab
    

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

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

    并启动 ipython

    ipython --profile=pylab
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-01
      • 1970-01-01
      • 2016-06-07
      • 1970-01-01
      相关资源
      最近更新 更多