pandas 像在 MATLAB 中一样在 IPython 中保存会话?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12504951/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Save session in IPython like in MATLAB?
提问by Navneet
It would be useful to save the session variables which could be loaded easily into memory at a later stage.
保存会话变量会很有用,这些变量可以在稍后阶段轻松加载到内存中。
回答by unutbu
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.
有关“会话日志记录和恢复”的更多信息,请参阅文档。
Note that this merely stores the commandsrun by IPython. It does not save the state of the IPython session. Restoring the session requires re-execution of the commands.
请注意,这仅存储IPython 运行的命令。它不保存 IPython 会话的状态。恢复会话需要重新执行命令。
If you set the PYTHONSTARTUPenvironment variable to point at a file called, say, startup.py:
如果您将PYTHONSTARTUP环境变量设置为指向名为的文件,例如startup.py:
PYTHONSTARTUP=/path/to/startup.py
then put the following in /path/to/startup.py:
然后将以下内容放入/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")
then IPython will call %logstart automatically whenever you start an interactive session.
那么每当您启动交互式会话时,IPython 都会自动调用 %logstart。
回答by A.E. Drew
Looking for something similar I came across save_ipython_variables:
寻找类似的东西我遇到了save_ipython_variables:
save-ipython-variableslets you ... save your global IPython variables to disk easily, and load them back into the global namespace when you need them again, even in a whole new IPython session.
save-ipython-variables让您...轻松地将全局 IPython 变量保存到磁盘,并在您再次需要它们时将它们加载回全局命名空间,即使在全新的 IPython 会话中也是如此。
I haven't had much chance to use it yet, but looks promising.
我还没有太多机会使用它,但看起来很有希望。
回答by Navaneethan Santhanam
I haven't tried this yet, but starting from AE Drew's answer, I found a possible alternative. Looks like IPython has a built in magic command that does this called %store:
我还没有尝试过,但是从 AE Drew 的回答开始,我找到了一个可能的选择。看起来 IPython 有一个内置的魔法命令来执行这个叫做%store:
%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:
%store 用于轻量级持久性的魔法。在 IPython 的数据库中存储变量、别名和宏。要在启动时自动恢复存储的变量,请将其添加到您的 ipython_config.py 文件中:
c.StoreMagic.autorestore = True
回答by Harshit Garg
There is also a magic command, history, that can be used to write all the commands/statements given by user.
还有一个魔法命令 ,history可用于编写用户给出的所有命令/语句。
Syntax : %history -f file_name.
Also %save file_name start_line-end_line, where star_line is the starting line number and end_line is ending line number. Useful in case of selective save.
语法:%history -f file_name. 此外%save file_name start_line-end_line,star_line 是起始行号,end_line 是结束行号。在选择性保存的情况下很有用。
%runcan be used to execute the commands in the saved file
%run可用于执行保存文件中的命令
回答by alpha_989
Not my solution, but this seems to be the closest solution, if you are using ipython: https://stackoverflow.com/a/28552465/4752883
不是我的解决方案,但这似乎是最接近的解决方案,如果您使用的是 ipython:https://stackoverflow.com/a/28552465/4752883

