Reputation: 2521
In ipython
, I know I can save aliases.
Is there similar way to save my own function, so that in is available next time I run ipython
?
lets say I have function:
def x(a,b):
print a+b
I tried using the %store
command:
%store x
stored x (function)
But next time I start ipython
it says:
name 'x' is not defined
Upvotes: 0
Views: 184
Reputation: 8144
I don't know if the exact feature you are asking exists in ipython.
What I do to handle this common case is to copy the functions in a .py file saved in the same folder as the notebook. Then, you can import the functions at the beginning of the notebook. In this way, several notebooks can use the functions that are defined only once. It is also easy to distribute the notebook and the .py file together (for collaborating or moving to different machine).
Alternatively you can put the functions in a ipython notebook. From a notebook, you can "import" another notebook like it was a .py file, just use a notebook file name without spaces or '-'. This approach is very similar to the .py file. The advantage is that you can add a more "rich" description of the functions (links, images, math formulas, etc...). The drawback is that you can't seamlessly import the functions in another python script.
The only drawback of these approaches is that if you have a complex structure of folders and subfolders containing notebooks, you need to copy (or link) the .py file in each subfolder. To avoid that, you can put your .py file in a single location and use absolute imports in the notebooks.
Upvotes: 1
Reputation: 527
When you start ipython
, you have to restore saved variables, aliases and functions using:
%store -r
There is however a chance this will not work, as there is apparently a bug
Upvotes: 0