Why can't I `print __doc__` in Python 3?

Question:

I have this code:

#!/usr/local/bin/python
"""

USAGE:

    apache_logs.py 

"""

import sys
import os


if __name__ == "__main__":
    if not len(sys.argv) > 1:
        print __doc__
        sys.exit(1)
    infile_name = sys.argv[1]

I get an error that says

Tue Jul 21{stevenhirsch@steven-hirschs-macbook-pro-2}/projects/python:-->./apache_logs.py 
  File "./apache_logs.py", line 17
    print __doc__
                ^
SyntaxError: invalid syntax

Why? The documentation I’ve read all seems to suggest it should work.

Asked By: ennuikiller

||

Answers:

What version of Python do you have? In Python 3, print was changed to work like a function rather than a statement, i.e. print('Hello World') instead of print 'Hello World'

I can recommend you to keep using Python 2.6 unless you’re doing some brand new production development. Python 3 is still pretty new.

Answered By: Blixt
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.