Importing Python Shell Code
Interactive sessions usually start from code that already exists — old scripts, notebook cells, or text pasted from documentation. IPython offers several magics for pulling shell code into the session: %load inserts the contents of a file into the next cell or input area, %paste executes a block that you copy to the clipboard, and %store pushes variables across sessions.
Loading a File with %load
%load accepts a file name, a URL, a module or function name, or a notebook cell reference. It does not execute anything immediately; instead it replaces the current input with the code, which you can inspect, adjust, and run afterward:
In [1]: %load preprocess.py
# Now the cell contains the contents of preprocess.py:
# import pandas as pd
# def clean(df): ...
In [2]: %run preprocess.py # evaluate once the code looks right
Loading straight from the network is equally natural and works inside a notebook where the shell can reach the internet:
In [3]: %load https://raw.githubusercontent.com/user/proj/master/demo.py
Pasting from the Clipboard
Copied code frequently contains prompt markers such as >>> or In [12]:. %paste strips those markers, keeps the block's indentation, and executes the cleaned result:
In [4]: %paste
>>> total = sum(range(10))
>>> total
## -- End pasted text --
45
Storing Variables Between Sessions
%store saves arbitrary variables to IPython's profile database so they survive a restart — handy for expensive objects (only small ones are practical here) and frequently used settings:
In [5]: name = "ipython"
In [6]: %store name
Stored 'name' (str)
In [7]: exit # restart the shell
$ ipython
In [1]: %store -r name
In [2]: name
Out[2]: 'ipython'
Summary
Bring external code in with %load for files, URLs, and module objects; run clipboard blocks cleanly with %paste, which removes prompt markers; and carry variables across restarts via %store and %store -r.