像在MATLAB中一样在IPython中保存会话? [英] Save session in IPython like in MATLAB?

查看:107
本文介绍了像在MATLAB中一样在IPython中保存会话?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

保存会话变量会很有用,这些变量可以在以后的阶段轻松加载到内存中。

It would be useful to save the session variables which could be loaded easily into memory at a later stage.

推荐答案

In [23]: %logstart /tmp/session.log
Activating auto-logging. Current session state plus future input saved.
Filename       : /tmp/session.log
Mode           : backup
Output logging : False
Raw input log  : False
Timestamping   : False
State          : active

In [24]: x = 1

In [25]: %logstop

In [26]: quit()
Do you really want to exit ([y]/n)? y

然后我们可以恢复会话:

Then we can restore the session with:

% ipython -log /tmp/session.log 
Activating auto-logging. Current session state plus future input saved.
Filename       : ipython_log.py
...

In [1]: x
Out[1]: 1

有关会话记录和恢复的更多信息,请参阅文档

For more on "Session logging and restoring" see the docs.

请注意,这只是存储命令由IPython运行。它不保存IPython会话的
状态。恢复会话需要重新执行
命令。

Note that this merely stores the commands run by IPython. It does not save the state of the IPython session. Restoring the session requires re-execution of the commands.

如果设置 PYTHONSTARTUP 环境变量指向一个名为 startup.py :

If you set the PYTHONSTARTUP environment variable to point at a file called, say, startup.py:

PYTHONSTARTUP=/path/to/startup.py

然后将以下内容放在/path/to/startup.py中:

then put the following in /path/to/startup.py:

try:
    # https://stackoverflow.com/a/5377051/190597 (Tom Dunham)
    __IPYTHON__
except NameError:
    pass
else:
    # https://stackoverflow.com/a/15898875/190597 (user2261139)
    from IPython import get_ipython
    ipython = get_ipython()
    ipython.magic("%logstart /tmp/session.log")

然后IPython将调用每当您开始交互式会话时,%logstart会自动启动。

then IPython will call %logstart automatically whenever you start an interactive session.

这篇关于像在MATLAB中一样在IPython中保存会话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆