title: Save Interactive Python (using script)
body: <p>I would like to save the input and the output of a Python session when providing the input as a file.</p>
  
  <p>In interactive mode, I would type a command of expressions and get their evaluation, as shown:</p>
  
  <pre class="lang-none prettyprint-override"><code>Python 2.7.11 (default, Mar  8 2016, 18:01:39)
  [GCC 4.2.1 Compatible Apple LLVM 7.0.2 (clang-700.1.81)] on darwin
  Type "help", "copyright", "credits" or "license" for more information.
  &gt;&gt;&gt; abs(3)
  3
  &gt;&gt;&gt; abs(-3)
  3
  </code></pre>
  
  <p>The text above is my desired output.</p>
  
  <p>When I write these two expressions in <code>p.py</code>:</p>
  
  <pre class="lang-none prettyprint-override"><code>abs(3)
  abs(-3)
  </code></pre>
  
  <p>and execute  <code>python p.py</code>, I don't get any output, which is fine because Python runs in non-iteractive mode. If I execute <code>python -i &lt; p.py</code>, I just get the output, not the input.</p>
  
  <p>So, I dediced to use the <code>script</code> command.</p>
  
  <p>However, with <code>script</code>, I get different results in MacOs and Linux (and none of them save what I would like). </p>
  
  <p>If I execute <code>script -q output.txt python -i &lt;p.py</code> on MacOs, file <code>output.txt</code>contains</p>
  
  <pre class="lang-none prettyprint-override"><code>abs(-3)
  abs(3)
  Python 2.7.11 (default, Mar  8 2016, 18:01:39)
  [GCC 4.2.1 Compatible Apple LLVM 7.0.2 (clang-700.1.81)] on darwin
  Type "help", "copyright", "credits" or "license" for more information.
  &gt;&gt;&gt; abs(-3)
  3
  &gt;&gt;&gt; abs(3)
  3
  </code></pre>
  
  <p>which is the desired output but with the input of <code>p.py</code> preppended to it.</p>
  
  <p>If I execute <code>script -q output.txt -c "python -i &lt;p.py"</code> on Linux, file <code>output.txt</code> contains</p>
  
  <pre class="lang-none prettyprint-override"><code>Python 2.7.3 (default, Jun 22 2015, 19:33:41)
  [GCC 4.6.3] on linux2
  Type "help", "copyright", "credits" or "license" for more information.
  &gt;&gt;&gt; 3
  &gt;&gt;&gt; 3
  </code></pre>
  
  <p>which is far from I would like.</p>
  
  <p>In brief: </p>
  
  <ul>
  <li>How could I save a file that reproduces the interactive Python session when redirecting input to it?</li>
  </ul>
  
  <p>Additionally,</p>
  
  <ul>
  <li>Why do the MacOs and Linux implementations of <code>script</code> work so differently?</li>
  </ul>