16

I'm dealing with some GB-sized numpy arrays in IPython. When I delete them, I definitely want them gone, in order to recover the memory. IPythons output cache is quite annoying there, as it keeps the objects alive even after deleting the last actively intended reference to them. I already set

c.TerminalInteractiveShell.cache_size = 0

in the IPython configuration, but this only disables caching of entries to _oh, the other variables like _, __ and so on are still created. I'm also aware of %xdel, but anyways, I'd prefer to disable it completely, as I rarely use the output history anyways, so that a plain del would work again right away.

wjandrea
  • 28,235
  • 9
  • 60
  • 81
Michael
  • 7,316
  • 1
  • 37
  • 63
  • 1
    Those large arrays get assigned to the cache (`_` etc) only if you display them, don't they? Assignment `x=largearray` and `largearray;` suppress that. – hpaulj Dec 28 '13 at 17:56
  • 1
    True, but for displaying them I always had to use print then instead of just typing the variable name. So I'm still back to the same question, how to avoid that. – Michael Dec 29 '13 at 08:57
  • Often it is more useful to display just the shape, or a slice of a large array (e.g. first 10 rows or columns), rather than the whole thing. That was especially true on old MATLAB which didn't use ellipsis when displaying large arrays. – hpaulj Dec 29 '13 at 16:28

4 Answers4

6

Looking at IPython/core/displayhook.py Line 209-214 I would say that it is not configurable. You could try making a PR to add an option to disable it totally.

Matt
  • 27,170
  • 6
  • 80
  • 74
  • 3
    This actually gave me the right idea! Look closer into the code, it checks for `_` being present in - depending on version - `__builtin__` or `IPython.core.py3compat.builtin_mod`. So after `__builtin__._ = True`, the history is gone! Thanks! – Michael Dec 29 '13 at 09:12
  • Oh, and I suppose you put that in your startup folder and then, it's definitive :-) – Matt Dec 29 '13 at 11:04
  • @Michael would you care to change your comment into an answer, maybe adding a little bit detail on what you did? I'd like to do the same thing as you but I'i'd rather not tinker with it and use (and upvode) canned solution. – jb. Jun 23 '14 at 12:52
  • @jb I added an answer below. – Michael Jul 11 '14 at 14:33
  • 1
    The answer is not longer working under Jupyter. Not even c.TerminalInteractiveShell.cache_size = 0 is working. How do I disable both Out and _? @Matt – Gioelelm Jan 25 '17 at 02:04
  • Well, the answer says there is nothing to do, that's not configurable. Which seem to still be true. `Out` seem to come from [here](https://github.com/ipython/ipython/blob/28d41caaeb06594e6fa7368499d0cdad4b81c158/IPython/core/history.py#L496) so not configurable either, unless someone send a patch and/or open an issue. – Matt Jan 25 '17 at 04:54
  • Try to set `c.InteractiveShell.cache_size = 0` in `.ipython/profile_default/ipython_config.py`. – Louis Yang Mar 01 '19 at 21:22
6

Enter

echo "__builtin__._ = True" > ~/.config/ipython/profile_default/startup/00-disable-history.py

and your history should be gone.

Edit:

Seems like the path to the config directory is sometimes a bit different, either ~/.config/ipython or just ~/.ipython/. So just check which one you got and adjust the path accordingly. The solution still works with jupyter console.

Michael
  • 7,316
  • 1
  • 37
  • 63
3

Seems that we can suppress the output cache by putting a ";" at the end of the line now.

See http://ipython.org/ipython-doc/stable/interactive/tips.html#suppress-output

phil
  • 2,558
  • 1
  • 19
  • 23
1

Create an ipython profile:

!ipython profile create

The output might be (for ipython v4.0):

[ProfileCreate] Generating default config file: '/root/.ipython/profile_default/ipython_config.py'
[ProfileCreate] Generating default config file: '/root/.ipython/profile_default/ipython_kernel_config.py'

Then add the line 'c.InteractiveShell.cache_size = 0' to the ipython_kernel_config.py file by

!echo 'c.InteractiveShell.cache_size = 0' >> /root/.ipython/profile_default/ipython_kernel_config.py

Load another ipython kernel and check if this work

In [1]: 123
Out[1]: 123

In [2]: _1
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-51-21553803e553> in <module>()
----> 1 _1

NameError: name '_1' is not defined

In [3]: len(Out)
Out[3]: 0
Louis Yang
  • 3,511
  • 1
  • 25
  • 24