gpt4 book ai didi

ipython - 在 IPython 中存储宏

转载 作者:行者123 更新时间:2023-12-02 18:11:46 26 4
gpt4 key购买 nike

根据文档,我应该能够定义一个宏并存储它。然后,该宏将在下次启动 IPython shell 时可用。但是,它不起作用:

In [4]: print "Foobarbatbizbuzzbonk"
Foobarbatbizbuzzbonk

In [5]: %macro foo 4
Macro `foo` created. To execute, type its name (without quotes).
=== Macro contents: ===
print "Foobarbatbizbuzzbonk"

In [6]: %store foo
Stored 'foo' (Macro)

In [7]: quit()

当我再次启动 IPython shell 时,没有宏:

In [1]: foo
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-1-d3b07384d113> in <module>()
----> 1 foo

NameError: name 'foo' is not defined

In [2]: %macro
Out[2]: []

有人知道为什么这不起作用吗?

最佳答案

我在几个不起眼的地方找到了这个问题的答案。

首先,在 $HOME/.ipython 中的自述文件中,它说:“有关配置 IPython 的更多信息,请执行:ipython config -h

这样做会产生大量帮助,包括以下建议:

To initialize a profile with the default configuration file, do::

$> ipython profile create

and start editing `IPYTHONDIR/profile_default/ipython_config.py`

此配置文件的旧文档位于此处: Configuring the ipython command line application 。最新的(截至 2020 年 1 月)位于 Configuration and customization 上经过大幅改进的部分。 .

最后,我在 storemagic 的文档中找到了答案。 [链接更新,2020 年 1 月]:

%store magic for lightweight persistence. Stores variables, aliases and macros in IPython’s database. To automatically restore stored variables at startup, add this to your ipython_config.py file:

c.StoreMagics.autorestore = True

添加该内容,重新启动 IPython 即可!有我的宏。酷!

关于ipython - 在 IPython 中存储宏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17693329/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com