导入错误:无法从'pynput.keyboard'导入名称'key'



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

我对此错误有问题:

    ImportError: cannot import name 'key' from 'pynput.keyboard' 
    (C:UsersrichardAppDataLocalProgramsPythonPython37-32libsite- 
    packagespynputkeyboard__init__.py)

你能告诉我,如何解决它吗?

我试图在谷歌上找到一些建议,但我什么也没找到。也许只是太愚蠢的"问题"。

这是未完成的代码,我想尝试它是否正常工作,然后出现了错误。

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:UsersrichardAppDataLocalProgramsPythonPython37-32libsite- 
packagespynputkeyboard__init__.py)
Process finished with exit code 1

它的Keykey

from pynput.keyboard import Key, Listener

在此处查看文档。

相关内容

  • 没有找到相关文章

最新更新