How to import the most recent version of the print function in Python 2.7.x?
<p>I have discovered that I can't use the <code>flush</code> argument in the new print function if I use Python 2.7.11. I have used:</p>

<pre><code>from __future__ import print_function

print('Hello', flush=True)
</code></pre>

<p>but it complains with the error:</p>

<pre><code>Traceback (most recent call last):
  File "print_future.py", line 3, in &lt;module&gt;
    print('Hello', flush=True)
TypeError: 'flush' is an invalid keyword argument for this function
</code></pre>

<p>After looking at <a href="https://docs.python.org/3/library/functions.html#print" rel="nofollow">the documentation for <code>print</code></a> I discovered that it doesn't work even though it is an argument. My inference is that it doesn't work because <code>flush</code> was only added in version 3.3, thus the special <code>from __future__ import</code> statement is probably importing an older version of the function. The comments suggest it's using 2.7.11 but I don't understand why it's doing this.</p>

<p>I did see <a href="http://stackoverflow.com/questions/27991443/need-python-3-4-version-of-print-from-future">Need Python 3.4 version of print() from __future__</a>, whose answer just wraps <code>print</code> and manually adds the <code>flush</code> parameter. Even though that works, it seems more of a hack than addressing the real problem, that we don't have the most recent version of print. </p>

<p>Is there a way to import specific versions of Python functions (I want to use the Python 3.5 <code>print</code> function, specifically) to my current Python script? If this is not possible, why not?</p>

<hr>

<p>It seems (surprisingly) its not clear to people how my question is different even though I wrote it. I will say it differently.</p>

<ol>
<li>It seems that my issue is that its importing the python function that I am not expecting (since it doesn't find the flush argument). Therefore, the most natural and the first thing I'd like to do and know is, which print function is it importing. The comments suggest its using 2.7.11 but I don't understand why its doing this.</li>
<li>I understand that the from future statement changes how my compiler works. It seems I assumed that since it was a future statement it also brought in the print function from a future release. It seems it only changes the behaviour of my interpreter. If I were able to see what print function its using I would know that its not importing a function from a future release but only acting like a future python interpreter. Thats what it seems but I don't know for sure and I'd like to know for sure whats going on.</li>
<li>Last but not least, is there not a natural way to import future python functions to my current python script? I want to use python 3.5 print statement, is it not possible to use that function apart from just making the compiler behave like a future python interpreter but also behave like a future python version? It was (at least to me) counter intuitive to have the parser act like future python but still act like a 2.7.11. The solution I want is not a wrapper but a import of the recent python. If this is not possible, then a answer to my question should explain why its not possible.</li>
</ol>

<ol>
<li><blockquote>
  <p>its importing the python function that I am not expecting</p>
</blockquote>

<p>I don't see why, this is the version of <code>print</code> <a href="https://docs.python.org/2/library/functions.html#print" rel="nofollow">that's documented in 2.7.x</a>.</p>

<blockquote>
  <p>which print function is it importing. </p>
</blockquote>

<p>The version bundled with 2.7.11, which is the version that was introduced in 3.0 (see <a href="https://www.python.org/dev/peps/pep-3105/" rel="nofollow">PEP-3105</a>).</p>

<blockquote>
  <p>The comments suggest its using 2.7.11 but I don't understand why its doing this</p>
</blockquote>

<p>...because that's the version of Python you're using? <code>from __future__ import ...</code> doesn't search your computer for alternative implementations, it uses the one that's bundled with the version of Python you're using. If you think about it this is inevitable, as otherwise future imports would fail on machines that don't have 3.x installed.</p></li>
<li><p>The majority of this made little sense to me, but:</p>

<blockquote>
  <p>I assumed that since it was a future statement it also brought in the print function from a future release.</p>
</blockquote>

<p>That's <strong>exactly what it's doing</strong>. You don't get to choose <em>which</em> future release, though. You get the version that was planned for release at the time it was added into <code>__future__</code>.</p></li>
<li><blockquote>
  <p>is there not a natural way to import future python functions to my current python script?</p>
</blockquote>

<p>Yes, and it's the way you're using, as long as that functionality is supported in <a href="https://docs.python.org/2/library/__future__.html" rel="nofollow"><code>__future__</code></a>.</p>

<blockquote>
  <p>is it not possible to use that function</p>
</blockquote>

<p>not the 3.3-onwards version of it in the version of Python that you're using, no*. If you want the functionality offered by more recent versions of Python, <em>use a more recent version of Python</em>. 2.x is running out of road.</p>

<blockquote>
  <p>It was (at least to me) counter intuitive to have the parser act like future python but still act like a 2.7.11</p>
</blockquote>

<p>It's not <em>"[acting] like a 2.7.11"</em>. It's using the print function from 3.x rather than the print statement from 2.x. If you try to <code>print 'hello'</code> after the import of <code>print_function</code>, you'll get a <code>SyntaxError</code> as you would in 3.x.</p>

<p><em>* assuming you're not planning to get into hacking your installation</em></p></li>
</ol>

python python-2.7 python-3.x printing flush