I find myself frequently using Python's interpreter to work with databases, files, etc -- basically a lot of manual formatting of semi-structured data. I don't properly save and clean up the useful bits as often as I would like. Is there a way to save my input into the shell (db connections, variable assignments, little for loops and bits of logic) -- some history of the interactive session? If I use something like script I get too much stdout noise. I don't really need to pickle all the objects -- though if there is a solution that does that, it would be OK. Ideally I would just be left with a script that ran as the one I created interactively, and I could just delete the bits I didn't need. Is there a package that does this, or a DIY approach?
当前回答
我不得不努力寻找答案,我对iPython环境非常陌生。
这是可行的
如果你的iPython会话是这样的
In [1] : import numpy as np
....
In [135]: counter=collections.Counter(mapusercluster[3])
In [136]: counter
Out[136]: Counter({2: 700, 0: 351, 1: 233})
你想保存从1到135的行,然后在同一个ipython会话上使用这个命令
In [137]: %save test.py 1-135
这将把你所有的python语句保存在当前目录下的test.py文件中(你启动ipython的地方)。
其他回答
此外,reinteraction为Python会话提供了一个类似笔记本的界面。
我再提一个建议: 世爵
它有历史日志和变量资源管理器。如果你用过MatLab,你就会发现其中的相似之处。
如果使用bpython,所有的命令历史都会默认保存到~/.pythonhist。
要保存命令以供以后重用,您可以将它们复制到python脚本文件中:
$ cp ~/.pythonhist mycommands.py
然后编辑该文件以清理它并将其放在Python路径下(全局或虚拟环境的site-packages,当前目录,在*.pth中提到,或其他方式)。
要将命令包含到你的shell中,只需从保存的文件中导入它们:
>>> from mycommands import *
有一种方法可以做到。将文件存储在~/.pystartup…
# Add auto-completion and a stored history file of commands to your Python
# interactive interpreter. Requires Python 2.0+, readline. Autocomplete is
# bound to the Esc key by default (you can change it - see readline docs).
#
# Store the file in ~/.pystartup, and set an environment variable to point
# to it: "export PYTHONSTARTUP=/home/user/.pystartup" in bash.
#
# Note that PYTHONSTARTUP does *not* expand "~", so you have to put in the
# full path to your home directory.
import atexit
import os
import readline
import rlcompleter
historyPath = os.path.expanduser("~/.pyhistory")
def save_history(historyPath=historyPath):
import readline
readline.write_history_file(historyPath)
if os.path.exists(historyPath):
readline.read_history_file(historyPath)
atexit.register(save_history)
del os, atexit, readline, rlcompleter, save_history, historyPath
然后在shell中设置环境变量PYTHONSTARTUP(例如在~/.bashrc中):
export PYTHONSTARTUP=$HOME/.pystartup
你也可以添加这个来获得免费的自动补全:
readline.parse_and_bind('tab: complete')
请注意,这只适用于*nix系统。因为readline只在Unix平台上可用。
对于那些使用spacemacs和python层附带的ipython的人来说,save magic会创建大量不必要的输出,因为不断的自动补全命令在后台工作,例如:
len(all_suffixes)
';'.join(__PYTHON_EL_get_completions('''len'''))
';'.join(__PYTHON_EL_get_completions('''all_substa'''))
len(all_substantives_w_suffixes)
';'.join(__PYTHON_EL_get_completions('''len'''))
';'.join(__PYTHON_EL_get_completions('''all'''))
';'.join(__PYTHON_EL_get_completions('''all_'''))
';'.join(__PYTHON_EL_get_completions('''all_w'''))
';'.join(__PYTHON_EL_get_completions('''all_wo'''))
';'.join(__PYTHON_EL_get_completions('''all_wor'''))
';'.join(__PYTHON_EL_get_completions('''all_word'''))
';'.join(__PYTHON_EL_get_completions('''all_words'''))
len(all_words_w_logograms)
len(all_verbs)
为了避免这种情况,只需保存ipython缓冲区,就像通常保存任何其他:spc f一样