4

I'm trying to get an IPython alias to persist, and according to docs the %store magic offers this feature. But it's not working:

$ echo 'print("hello world!")' > test.py
$ ipython
In [1]: alias potato python /tmp/test.py

In [2]: potato
hello world!

In [3]: %store potato
Alias stored: potato (python /tmp/test.py)

In [4]: 
Do you really want to exit ([y]/n)? 
$ ipython
In [1]: potato
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-1-e561f9248d75> in <module>()
----> 1 potato

NameError: name 'potato' is not defined

What's missing?

wim
  • 338,267
  • 99
  • 616
  • 750

3 Answers3

10

You need to run %store -r to retrieve stored variables (and aliases).

Of course, you can add this to your ipython startup script.

ecatmur
  • 152,476
  • 27
  • 293
  • 366
  • 1
    OK. For ubuntu 13.10, it worked by adding my magic into `~/.config/ipython/profile_default/startup/wim.ipy` – wim Jan 06 '14 at 15:44
  • Thanks @wim, didn't know about the possibility to even use startup ipy-files (only knew to use normal python files), but this makes it very easy for me as the `store -r` doesnt work for me somehow – tim Jun 11 '14 at 13:26
  • It is possible to automatically store all variable in ipython? – BND Sep 21 '20 at 14:46
1

You can also restore in a regular script, for instance if your IDE (Spyder) doesn't support the ipython_config.py file:

from IPython import get_ipython
ipython = get_ipython()
ipython.magic("store -r")

(Put this in a file that's called in the Startup tab of Spyder's IPython configuration. This took me way too long to figure out.)

endolith
  • 25,479
  • 34
  • 128
  • 192
0

@ecatmur's solution worked for me. Thanks! I want only add an example how to add a startup script. Just add .ipy file to .ipython/profile_default/startup/ directory with following content %store -r:

[ikors@localhost ~]$ cat .ipython/profile_default/startup/startup_script.ipy
%store -r
ikors
  • 1
  • 1