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)"
]
}