1

When I am in the Python3 interpreter and type the three input statements below, I see "1" displayed twice, as displayed output from the second and third statements.

a = 1
a`
print(a)

If these statements are in a file, say test.py, and I call python3 test.py, I only see the output from the call to print. I am familiar with subprocess, and it also only gives me a single 1. Yet if I do the same in a Jupyter notebook, I see both of the returned 1s and both are saved in the notebook. I know I can programmatically run the Jupyter notebook in batch mode.

Can I do something similar from Python without resorting to using Jupyter? That is, I want all the interpreter output, not just that from print.

Update

You can see how a Jupyter notebook stores the regular data output and the stdout output:

  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n"
     ]
    }
   ],
   "source": [
    "print(a)"
   ]
  }
Bob Sutor
  • 71
  • 5
  • 1
    This is a feature of the IPython interactiveshell. See [`InteractiveShell.ast_node_interactivity`](https://ipython.readthedocs.io/en/stable/config/options/kernel.html#configtrait-InteractiveShell.ast_node_interactivity) – Trenton McKinney Aug 20 '20 at 18:31
  • Thank you, but can I get the output without using IPython? – Bob Sutor Aug 20 '20 at 18:36
  • 1
    Not that I'm able to find. – Trenton McKinney Aug 20 '20 at 18:43
  • 1
    `a` prints the value of `a` only an interactive shell. Running it from a file won't print the output. Seeing as how this behavior is only seen in an interactive shell, I don't think there's a way to not use one. – Pranav Hosangadi Aug 20 '20 at 18:49
  • I may resort to generating the JSON for input to a batch Jupyter notebook run, save the output, and then grab all the input from the resulting JSON. – Bob Sutor Aug 20 '20 at 18:55
  • 1
    Worth noting that even in Jupyter you won't get the output you're expecting if those are all in the same code block. Do you want to split your code into as many such blocks as possible? – Hans Musgrave Aug 20 '20 at 19:04
  • 1
    @HansMusgrave _Worth noting that even in Jupyter you won't get the output you're expecting if those are all in the same code block._ as noted in my first comment, this is controlled by a setting. [How to display full output in Jupyter, not only last result?](https://stackoverflow.com/questions/36786722) – Trenton McKinney Aug 20 '20 at 19:10
  • Yes, I actually used separate code blocks in Jupyter. – Bob Sutor Aug 20 '20 at 19:40

0 Answers0