【问题标题】:How can I configure IPython to issue the same "magic" commands at every startup?如何配置 IPython 在每次启动时发出相同的“魔术”命令?
【发布时间】:2017-01-10 19:42:46
【问题描述】:

每当我从终端呼叫ipython 时,我希望能够使用%cd "default_dir"%matplotlib。我尝试将其写入.ipython/profile_default/startup/file.py 的 .py 文件中,但会导致以下错误:

[TerminalIPythonApp] WARNING | Unknown error in handling startup files:
  File "/Users/<name>/Dropbox/.ipython/profile_default/startup/startup.py", line 18
    %cd "~/Dropbox/"
    ^
SyntaxError: invalid syntax

【问题讨论】:

  • @Harrison 请看编辑;也许你误解了我,或者我的要求对你来说似乎很明显。
  • 不,绝对不是——至少我似乎在那个问题中找不到它

标签: python python-3.x ipython


【解决方案1】:

您只需要在启动脚本中使用魔法:

get_ipython().magic('cd ~/Dropbox')
get_ipython().magic('matplotlib')

把它放在你的启动脚本的内容中,它应该会做你需要的魔法✨?✨

【讨论】:

  • 您能否简要解释一下您的答案为何有效,或者将我链接到任何有用的资源?似乎找不到任何东西。
  • 我搜索了“ipython pass variable to magic function”之类的东西,结果找到了this answer,这正是你想要的结果。
【解决方案2】:

我只是想详细说明韦恩的答案,但没有足够的声誉来发表评论。您可以在启动脚本中包含以下内容来运行所需的魔术命令

from IPython.core import getipython

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

模块参考在这里Ipython module

要在终端运行上述启动,请执行此操作

ipython -i startup.py

【讨论】:

  • 非常有帮助。对于那些搜索,请注意,您可以通过键入 get_ipython().profile_dir.startup_dir 来本地化启动脚本文件夹
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多