Can a python script access variables defined in an interactive session?

interactive, ipython, python

Solution

Yes, from the run command documentation: if you use `%run -i` it will run the script in your existing interactive session's namespace instead of a clean one, so it will have access to defined variables.

If you want similar in the standard python shell, you can run it with execfile: `execfile('my_script.py')`

Problem

So, for instance, I'm in an ipython session, and I have a variable, ``` var = [3,5,6] ``` defined in the ipython session, that I want to do something with by running a script, e.g.: ``` # my_script plot(var) ``` so I want typing ``` %run my_script.py ``` from the interactive session to plot var, as if I had typed: ``` plot(var) ``` within the interactive session. Is this possible? How?

Original source