Petr Laškevič
asked on
line 10, in <module> with keyboard.Listener(NameError: name 'keyboard' not defined pynput although it is installed and everything was done according to their documentation
I wanted to detect keyboard with pynput, so I copied and pasted a demo from their site. Here it is:
from pynput import keyboard
def on_press(key):
try:
print('alphanumeric key {0} pressed'.format(
key.char))
except AttributeError:
print('special key {0} pressed'.format(
key))
def on_release(key):
print('{0} released'.format(
key))
if key == keyboard.Key.esc:
# Stop listener
return False
# Collect events until released
with keyboard.Listener(
on_press=on_press,
on_release=on_release) as listener:
listener.join()
But I get this error, the file is named differently than the module:
Traceback (most recent call last):
File "C:\Users\Andrey\Documents \Python\12 3.py", line 1, in <module>
from pynput import keyboardFile
"C:\Users\Andrey\AppData\L ocal\Progr ams\Python \Python36- 32\pynput. py", line 10, in <module>
with keyboard.Listener(NameErro r: name 'keyboard' is not defined
from pynput import keyboard
def on_press(key):
try:
print('alphanumeric key {0} pressed'.format(
key.char))
except AttributeError:
print('special key {0} pressed'.format(
key))
def on_release(key):
print('{0} released'.format(
key))
if key == keyboard.Key.esc:
# Stop listener
return False
# Collect events until released
with keyboard.Listener(
on_press=on_press,
on_release=on_release) as listener:
listener.join()
But I get this error, the file is named differently than the module:
Traceback (most recent call last):
File "C:\Users\Andrey\Documents
from pynput import keyboardFile
"C:\Users\Andrey\AppData\L
with keyboard.Listener(NameErro
can you post the contents of the file "C:\Users\Andrey\AppData\L ocal\Progr ams\Python \Python36- 32\pynput. py" as shown in your traceback?
How did you install pynput?
I installed using pip3:
I installed using pip3:
C:\Windows\system32>pip3 install pynput
Collecting pynput
Downloading https://files.pythonhosted.org/packages/80/b6/b3558caf276458b123217a1bf8772c75637e500591ece7c3295110dab19e/pynput-1.4-py2.py3-none-any.w
hl (72kB)
100% |████████████████████████████████| 81kB 656kB/s
Requirement already satisfied: six in c:\program files\python36\lib\site-packages (from pynput) (1.11.0)
Installing collected packages: pynput
Successfully installed pynput-1.4
C:\Windows\system32>py -3
Python 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 08:06:12) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from pynput import keyboard
>>> help(keyboard)
Help on package pynput.keyboard in pynput:
NAME
pynput.keyboard - The module containing keyboard classes.
DESCRIPTION
See the documentation for more information.
PACKAGE CONTENTS
_base
_darwin
_win32
_xorg
FILE
c:\program files\python36\lib\site-packages\pynput\keyboard\__init__.py
>>> quit()
C:\Windows\system32>
ASKER
The way how I finally solved it, was that I deleted everything what was named pynput and downloaded pynput once again through pip.
This question needs an answer!
Become an EE member today
7 DAY FREE TRIALMembers can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
ASKER