This error message means that you are attempting to use Python 3 to follow an example or run a program that uses the Python 2 print
statement:
(此错误消息表示您尝试使用Python 3遵循示例或运行使用Python 2 print
语句的程序:)
print "Hello, World!"
The statement above does not work in Python 3. In Python 3 you need to add parentheses around the value to be printed:
(上面的语句在Python 3中不起作用。在Python 3中,您需要在要打印的值周围添加括号:)
print("Hello, World!")
“SyntaxError: Missing parentheses in call to 'print'” is a new error message that was added in Python 3.4.2 primarily to help users that are trying to follow a Python 2 tutorial while running Python 3.
(“ SyntaxError:对'print'的调用中缺少括号”是Python 3.4.2中新增的一条错误消息,主要用于帮助试图在运行Python 3时遵循Python 2教程的用户。)
In Python 3, printing values changed from being a distinct statement to being an ordinary function call, so it now needs parentheses:
(在Python 3中,打印值从一个独特的语句变为一个普通的函数调用,因此现在需要括号:)
>>> print("Hello, World!")
Hello, World!
In earlier versions of Python 3, the interpreter just reports a generic syntax error, without providing any useful hints as to what might be going wrong:
(在Python 3的早期版本中,解释器仅报告一般语法错误,而没有提供任何有用的提示来提示可能出了什么问题:)
>>> print "Hello, World!"
File "<stdin>", line 1
print "Hello, World!"
^
SyntaxError: invalid syntax
As for why print
became an ordinary function in Python 3, that didn't relate to the basic form of the statement, but rather to how you did more complicated things like printing multiple items to stderr with a trailing space rather than ending the line.
(至于为什么 print
在Python 3中成为普通函数,与语句的基本形式无关,而是与您如何做更复杂的事情类似,例如将多个项目打印到带有尾部空格的stderr而不是结束行。)
In Python 2:
(在Python 2中:)
>>> import sys
>>> print >> sys.stderr, 1, 2, 3,; print >> sys.stderr, 4, 5, 6
1 2 3 4 5 6
In Python 3:
(在Python 3中:)
>>> import sys
>>> print(1, 2, 3, file=sys.stderr, end=" "); print(4, 5, 6, file=sys.stderr)
1 2 3 4 5 6
Starting with the Python 3.6.3 release in September 2017, some error messages related to the Python 2.x print syntax have been updated to recommend their Python 3.x counterparts:
(从2017年9月的Python 3.6.3版本开始,一些与Python 2.x打印语法相关的错误消息已更新,以推荐与之对应的Python 3.x:)
>>> print "Hello!"
File "<stdin>", line 1
print "Hello!"
^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print("Hello!")?
Since the "Missing parentheses in call to print" case is a compile time syntax error and hence has access to the raw source code, it's able to include the full text on the rest of the line in the suggested replacement.
(由于“在打印输出中缺少括号”情况是编译时语法错误,因此可以访问原始源代码,因此可以在建议的替换内容中将其余行中的全文包括在内。)
However, it doesn't currently try to work out the appropriate quotes to place around that expression (that's not impossible, just sufficiently complicated that it hasn't been done). (但是,它目前并未尝试找出适合该表达式的引号(这不是不可能的,只是足够复杂以至于尚未完成)。)
The TypeError
raised for the right shift operator has also been customised:
(还为右移运算符引发了TypeError
:)
>>> print >> sys.stderr
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for >>: 'builtin_function_or_method' and '_io.TextIOWrapper'. Did you mean "print(<message>, file=<output_stream>)"?
Since this error is raised when the code runs, rather than when it is compiled, it doesn't have access to the raw source code, and hence uses meta-variables ( <message>
and <output_stream>
) in the suggested replacement expression instead of whatever the user actually typed.
(由于此错误是在代码运行时(而不是在编译时)引发的,因此它无法访问原始源代码,因此在建议的替换表达式中使用元变量( <message>
和<output_stream>
)用户实际键入的内容。)
Unlike the syntax error case, it's straightforward to place quotes around the Python expression in the custom right shift error message. (与语法错误的情况不同,在自定义右移错误消息中将引号放在Python表达式周围很简单。)