Link to home
Start Free TrialLog in
Avatar of Petr Laškevič
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\123.py", line 1, in <module>
    from pynput import keyboardFile
  "C:\Users\Andrey\AppData\Local\Programs\Python\Python36-32\pynput.py", line 10, in <module>
    with keyboard.Listener(NameError: name 'keyboard' is not defined
Avatar of Petr Laškevič
Petr Laškevič

ASKER

Please, this may seem like a really dumb question but I would really appreciate any help. Additional information: the computer is running Windows 10 and it was manufactured by Lenovo. I use Python 3.6 and I have tried to download pynput multiple times. Everything seems as if it is downloading, since Windows Powershell tells me Requirement already satisfied. I also tried downloading other versions of pynput, but the result is the same: everything seems downloaded, but I get that error.
can you post the contents of the file "C:\Users\Andrey\AppData\Local\Programs\Python\Python36-32\pynput.py" as shown in your traceback?
How did you install pynput?
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>

Open in new window

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 TRIAL
Members 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.