ImportError: cannot import name 'key' from 'pynput.keyboard'

ImportError: cannot import name 'key' from 'pynput.keyboard'

首先,如果这个问题很愚蠢,我想道歉。

我对这个错误有疑问:

    ImportError: cannot import name 'key' from 'pynput.keyboard' 
    (C:\Users\richard\AppData\Local\Programs\Python\Python37-32\lib\site- 
    packages\pynput\keyboard\__init__.py)

你能告诉我如何解决吗?:

我试图在 google 上找到一些建议,但我什么也没找到。可能是太笨了"problem".

这是未完成的代码,我想试试它是否有效,然后出现错误。

import pynput

from pynput.keyboard import key, Listener

count = 0
keys = []

def on_press(key):
    global keys, count
    print("{0} pressed".format(key))

def write_file(keys):
    with open("log.txt", "w") as f:
        for key in keys:
            f.write(key)

def on_realease(key):
    if key == Key.esc:
        return False

with Listener (on_press=on_press, on_release=on_realease) as listener:
    listener.join()

这就是整个问题:

Traceback (most recent call last):
  File "C:/Users/richard/AppData/Local/Programs/Python/Python37- 
  32/Logger.py", line 3, in <module>
    from pynput.keyboard import key, Listener
ImportError: cannot import name 'key' from 'pynput.keyboard' 
(C:\Users\richard\AppData\Local\Programs\Python\Python37-32\lib\site- 
packages\pynput\keyboard\__init__.py)

Process finished with exit code 1

Key 不是 key

from pynput.keyboard import Key, Listener

查看文档 here