簡體   English   中英

無法從輸入中導入協議

[英]can not import Protocol from typing

我在 Windows 10 上全新安裝了 Python 3.7-32。

我想嘗試 Protocols python 方法,我接下來做:

文件 test_protocols.py 只有一行:

from typing import Protocol

然后:

>python test_protocols.py

我還有下一條需要解釋的錯誤消息:

Traceback (most recent call last):
  File "test_protocols.py", line 1, in <module> 
    from typing import Protocol
ImportError: cannot import name 'Protocol' from 'typing' (C:\Programing\Python\Python37-32\lib\typing.py)

我做錯了什么?

也許我讀錯了 PEP-0544,但從我的角度來看,我做的和記錄的一樣。

截至 2019 年 1 月 20 日, PEP 544的狀態為Draft 據我了解,它還沒有在 CPython 中實現。

UPD :它應該從 Python 3.8 開始工作,請嘗試更新。

PEP 544實施部分,它說

mypy類型檢查器完全支持協議(以一些已知錯誤為模)。 這包括在結構上處理所有內置協議,例如Iterable PyPI 上的typing_extensions模塊中提供了協議的運行時實現。

因此,在您的代碼中,添加from typing_extensions import Protocol

如果它在輸入中不存在,請執行

pip install typing_extensions

from typing_extensions import Protocol

根據操作系統和 Python 版本協議類可能在打字模塊或打字擴展中。

暫無
暫無

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

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