簡體   English   中英

Python 中的“SyntaxError: Missing parentheses in call to 'print'”是什么意思?

[英]What does "SyntaxError: Missing parentheses in call to 'print'" mean in Python?

當我嘗試在 Python 中使用print語句時,它給了我這個錯誤:

>>> print "Hello, World!"
  File "<stdin>", line 1
    print "Hello, World!"
                        ^
SyntaxError: Missing parentheses in call to 'print'

這意味着什么?

此錯誤消息意味着您正在嘗試使用 Python 3 來遵循示例或運行使用 Python 2 print語句的程序:

 print "Hello, World!"

上面的語句在 Python 3 中不起作用。在 Python 3 中,您需要在要打印的值周圍添加括號:

print("Hello, World!")

“SyntaxError: Missing parentheses in call to 'print'”是 Python 3.4.2 中添加的新錯誤消息,主要是為了幫助在運行 Python 3 時嘗試遵循 Python 2 教程的用戶。

在 Python 3 中,打印值從一個不同的語句變為一個普通的函數調用,所以它現在需要括號:

>>> print("Hello, World!")
Hello, World!

在 Python 3 的早期版本中,解釋器只報告一個通用語法錯誤,而沒有提供任何有用的提示來說明可能出現的問題:

>>> print "Hello, World!"
  File "<stdin>", line 1
    print "Hello, World!"
                        ^
SyntaxError: invalid syntax

至於為什么print在 Python 3 中成為一個普通函數,這與語句的基本形式無關,而是與您如何執行更復雜的事情有關,例如使用尾隨空格而不是結束行將多個項目打印到 stderr。

在 Python 2 中:

>>> import sys
>>> print >> sys.stderr, 1, 2, 3,; print >> sys.stderr, 4, 5, 6
1 2 3 4 5 6

在 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

從 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!")?

由於“打印調用中缺少括號”的情況是編譯時語法錯誤,因此可以訪問原始源代碼,因此它能夠在建議的替換中包含該行其余部分的全文。 但是,它目前並未嘗試計算出適當的引號來放置在該表達式周圍(這並非不可能,只是足夠復雜以至於尚未完成)。

為右移運算符引發的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>)"?

由於此錯誤是在代碼運行時而不是在編譯時引發的,因此它無法訪問原始源代碼,因此在建議的替換表達式中使用元變量( <message><output_stream> )用戶實際輸入的任何內容。 與語法錯誤情況不同,在自定義右移錯誤消息中的 Python 表達式周圍放置引號很簡單。

不幸的是,舊的xkcd 漫畫不再是最新的了。

https://imgs.xkcd.com/comics/python.png

從 Python 3.0 開始,您必須編寫:

print("Hello, World!")

還有人必須寫那個antigravity庫:(

從 Python 2 到 Python 3 的語法發生了變化。在 Python 2 中,

print "Hello, World!" 

可以,但在 Python 3 中,使用括號作為

print("Hello, World!")

這與 Scala 的語法等價,並且接近於 Java。

基本上,從 Python 3.x 開始,您需要使用帶括號的print

Python 2.x :打印“指環王”

Python 3.x :打印(“指環王”)


解釋

print2.x中的一個語句,但它是3.x中的一個函數 現在,這有很多很好的理由。

  1. 使用 Python 3.x 的函數格式,在打印多個以逗號分隔的項目時具有更大的靈活性。
  2. 您不能在聲明中使用參數飛濺。 在 3.x 中,如果您有一個要使用分隔符打印的項目列表,您可以這樣做:
>>> items = ['foo', 'bar', 'baz']
>>> print(*items, sep='+')
foo+bar+baz
  1. 您不能覆蓋語句。 如果你想改變 print 的行為,你可以在它是一個函數時這樣做,而不是在它是一個語句時。

如果您的代碼應該在 Python 2 和 3 中工作,您可以通過在程序開頭加載它來實現:

from __future__ import print_function   # If code has to work in Python 2 and 3!

然后你可以用 Python 3 的方式打印:

print("python")

如果您想在不創建新行的情況下打印某些內容 - 您可以這樣做:

for number in range(0, 10):
    print(number, end=', ')

在 Python 3 中,您只能打印為:

print("STRING")

但在 Python 2 中,括號不是必需的。

我還可以補充一點,我對Python2.7Python3之間的語法變化了如指掌,並且我的代碼被正確地編寫為print("string")甚至print(f"string") ...

但是經過一段時間的調試后,我意識到我的 bash 腳本正在調用 python,例如:

python 文件名.py

默認情況下使用python2.7調用我的python腳本的效果是錯誤的。 所以我將我的 bash 腳本更改為:

python3 文件名.py

哪個粗略使用 python3 運行修復錯誤的腳本。

除了這里的直接答案之外,還應該注意 python 2 和 3 之間的另一個關鍵區別。官方的 python wiki幾乎涵蓋了所有主要區別,並重點關注何時應該使用任何一個版本。 這篇博文也很好地解釋了當前的 Python 世界以及遷移到 Python 3 的未解之謎。

據我所知,你已經開始學習 Python 語言了。 在繼續使用 python 3 路線之前,您應該考慮上述文章。 您不僅需要更改一些語法,還需要考慮哪些包可供您使用(python 2 的優勢)以及可以在代碼中進行的潛在優化(python 3 的優勢) .

print('Hello, World!')

您使用的是 python 3,打印時需要括號。

所以我收到了這個錯誤

from trp import BoundingBox, Document
File "C:\Users\Kshitij Agarwal\AppData\Roaming\Python\Python39\site-packages\trp\__init__.py", line 31
print ip
      ^ 
SyntaxError: Missing parentheses in call to 'print'. Did you mean print(ip)?

這是一個 Python 包錯誤,其中使用了 Python2,您可能正在 Python3 上運行它。

一種解決方案可能是將 Python2 print something轉換為 Python3 print(something)對於包文件夾中每個文件中的每一行,這不是一個好主意😅。 我的意思是,你可以做到,但仍然有更好的方法。

為了執行相同的任務,Python 中有一個名為2to3的包,它將 Python2 腳本轉換為 Python3 腳本。 要安裝它,請在終端中執行👇 命令。

pip install 2to3

然后將終端中的目錄更改為包文件所在的位置,在我的情況下 - C:\Users\Kshitij Agarwal\AppData\Roaming\Python\Python39\site-packages\trp

現在執行命令👇

2to3 . -w

,該目錄中的所有Python2文件都將轉換為Python3

注意:- 上述命令也適用於其他操作系統。 只有 Python 包路徑會因系統而異。

print "text"不是在 python 中打印文本的方式,因為這不起作用print("text")將在命令行中的屏幕上打印所述文本

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM