1

I'm relatively new to NumPy/SciPy and IPython.

To execute a python script in the python interactive mode, we may use the following commands.

>>> import os
>>> os.system('executable.py')

Then the print outputs can be seen from the python prompt.

But the same idea doesn't work with IPython notebook.

In [64]:

import os
os.system('executable.py')

Out[64]:
0

In this case, I cannot see any print outputs. The notebook only tells weather execution was successful or not. Are there any ways to see the outputs when I use IPython notebook?

chanwcom
  • 4,420
  • 8
  • 37
  • 49
  • Can you import the file's contents and run from there? Seems a little silly to shell out to system when you're calling python code from python. – a p Dec 18 '15 at 20:52
  • Possible dupe of https://stackoverflow.com/questions/27682754/stdout-in-ipython-notebook-vs-cli-ipython – Cong Ma Dec 18 '15 at 20:55

1 Answers1

2

Use the magic function %run:

%run executable.py

This properly redirects stdout to the browser and you will see the output from the program in the notebook.

It gives you both, the typical features of running from command line plus Python tracebacks if there is exception.

Parameters after the filename are passed as command-line arguments to the program (put in sys.argv). Then, control returns to IPython's prompt.

This is similar to running at a system prompt python file args, but with the advantage of giving you IPython's tracebacks, and of loading all variables into your interactive namespace for further use (unless -p is used, see below).

The option -t times your script. With -d it runs in the debugger pdb. More nice options to explore.

Mike Müller
  • 82,630
  • 20
  • 166
  • 161