title: function to write on stderr with python2 and python3
body: <p>I'm writing a program that ought to work with both python2 and python3.
  For this, I would like to have a function to write to stderr that works with both python versions.</p>
  
  <p>Ideal I think would be something like:</p>
  
  <pre><code>def writeStdErr(message):
      if sys.version_info &gt;= (3, 0):
          print(message, end = "", file = sys.stderr)
      else:
          sys.stderr.write(message)
  </code></pre>
  
  <p>the problem with this that python2 is, that print isn't a function, so I get</p>
  
  <pre><code>    print(message, end = "", file = sys.stderr)
                         ^
  SyntaxError: invalid syntax
  </code></pre>
  
  <p>I could get rid of this by just adding eval:</p>
  
  <pre><code>def writeStdErr(message):
      if sys.version_info &gt;= (3, 0):
          eval('print(message, end = "", file = sys.stderr)')
      else:
          sys.stderr.write(message)
  </code></pre>
  
  <p>however, I dislike this solution; I think it's general a bad idea to use eval.</p>
  
  <p>Does anyone knows something better/has a better solution?</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>For anyone having the same problem in future, the following things seem works:</p>
  
  <pre><code>def writeStdErr(message):
      sys.stderr.write(message)
  </code></pre>
  
  <p>or</p>
  
  <pre><code>from __future__ import print_function
  import sys
  def writeStdErr(message):
      print(message, file=sys.stderr)
  </code></pre>
  
  <p>Thanks to all answers</p>