0

I am trying to use a simple event listener.

This is what I have so far:

import keyboard 
while True:
    if keyboard.is_pressed('p'):
        print('You pressed P')

I get the error:

AttributeError: module 'keyboard' has no attribute 'is_pressed'

When I check the documentation, I see that is_pressed exists.

Is there something wrong with the library or am I doing something wrong?

Reza Saadati
  • 5,018
  • 4
  • 27
  • 64
  • It works for me, just like you typed. – Vinícius Figueiredo Jul 06 '17 at 20:49
  • 1
    Have you made sure to properly install the keyboard module and such cause it seemed to work for me. – Professor_Joykill Jul 06 '17 at 20:50
  • 1
    Are you sure you're importing the right module? Try `print(keyboard.__file__)` to see what you're importing. – Aran-Fey Jul 06 '17 at 20:51
  • @Rawing I get `AttributeError: module 'keyboard' has no attribute '__file__'`. I am using Python version 3.6.1. Could that be an issue? I have downloaded keyboard from git. It's on my project directory. And I use Windows. – Reza Saadati Jul 06 '17 at 21:00
  • 1
    @RezaSaadati I believe that's the problem -- try doing `sudo pip install keyboard` in command line as mentioned in their docs. Cloning the repository won't hook it into python's package manager. If you want it to work with your local repository, see [this](https://stackoverflow.com/questions/9059699/python-use-a-library-locally-instead-of-installing-it) – aug Jul 06 '17 at 21:01
  • 1
    Instead of downloading it from git, why not install it with pip? Run `pip install keyboard` in an admin terminal. – Aran-Fey Jul 06 '17 at 21:02
  • @aug thank you very much! That was definitely the point and it works great now! – Reza Saadati Jul 06 '17 at 21:17
  • 1
    @Rawing thank you for this info! I used `py -m pip install keyboard` and it works like a charm! – Reza Saadati Jul 06 '17 at 21:18
  • Mine returns `/Python/2.7/lib/python/site-packages/pynput/keyboard/__init__.pyc`? When i print my keyboard, but the is_pressed command doesnt work? – Otto Apr 24 '20 at 07:18

2 Answers2

2

I'll add it as a formal answer anyways. You mentioned that you git clone the repository. I guess this is confusing since the README says you can either do pip install or git clone to install the package.

When you do import, it tells Python to go through it's package manager to find the module and these need to be installed on your end. pip is the package management system that Python uses

If you want to install a package that you have as a local folder (i.e. if you wanted to test a local module) you can do so see here

aug
  • 11,138
  • 9
  • 72
  • 93
1

please try to rename your document. don't put name as "keyboard.py"