8

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?

2
  • If you define functions in your script and want to call them on variables in your session, you can do import scriptname (assuming scriptname.py) and then call scriptname.plot(var), or similar. Commented Feb 19, 2015 at 21:07
  • That's not really what I'm after. I just want to make my script behave exactly as if it's contents were copy/pasted into the session. Commented Feb 19, 2015 at 22:11

1 Answer 1

8

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')

Sign up to request clarification or add additional context in comments.

1 Comment

Is something similar possible in python (no ipython)?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.