1

I use IPython as my interactive shell and I almost always run two commands at the start - something like:

%run helper_functions.py
data = import_data('file_with_data')

I would like a notation to run these two at the same time (so I could recall them with an "up" arrow from history) akin to what echo 1 ; echo 2 does in Bash.

I am aware that I can start IPython running with a script as per How can I start IPython running a script? .

Peter Mortensen
31.3k22 gold badges110 silver badges134 bronze badges
asked Jun 24, 2015 at 18:51
1
  • from file_that_runs_helper_functions_and_defines_data import data Commented Jun 24, 2015 at 18:58

1 Answer 1

0

Semicolon is a valid statement separator in Python, the same as the newline, so you can use it with regular commands:

a=5; a=a+1; print "This is a: %s" % a

Anyway, as pointed out by tobias_k, this is not working with "run" which is a special IPython macro (see the IPython built-in magic commands) and changes the command line interpretation. Instead of %run helper_functions.py; data = import_data('file_with_data'), you could use something almost equivalent:

__builtin__.execfile('helper_functions.py') ; data = import_data('file_with_data')

You don't have the trace in IPython, but still your script is executed and the variables are in your interactive namespace.

Peter Mortensen
31.3k22 gold badges110 silver badges134 bronze badges
answered Jun 24, 2015 at 18:58
Sign up to request clarification or add additional context in comments.

2 Comments

This does not seem to work. ; must not be directly after file name, and data seems not to be defined afterwards.
a = 1, b = 2 indeed does work, but the magic %run seems to cause problems, when I then run %run stuff.py ; b = 3, b is still 2.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.