gpt4 book ai didi

python - 导入错误: cannot import name 'key' from 'pynput.keyboard'

转载 作者:行者123 更新时间:2023-11-30 22:00:38 26 4
gpt4 key购买 nike

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

我遇到此错误:

    ImportError: cannot import name 'key' from 'pynput.keyboard' 
(C:\Users\richard\AppData\Local\Programs\Python\Python37-32\lib\site-
packages\pynput\keyboard\__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:\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 .

关于python - 导入错误: cannot import name 'key' from 'pynput.keyboard' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54262808/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com