1
import pynput
from pynput.keyboard import Key, Listener
def keyenter(key):
    print("{0} pressed".format(key))
def keyexit(key):
    if key == Key.esc:
       return False
with Listener(keyenter=keyenter, keyexit=keyexit) as listener:
    listener.join()

I keep getting the error -- ModuleNotFoundError: No module named 'pynput'

I've been at this for a while. Even had a go at online IDE's such as online-python.com, but threw the same error.

There are similar threads on stackoverflow but none seem to have a solid fix/guide to solve this.

  • Welcome to Stack Overflow. In your own words, when you write `import pynput`, exactly why do you think that should do anything at all? What source material did you read in order to hear about `pynput`, and what did that material tell you about how to use it? – Karl Knechtel Dec 12 '21 at 07:45
  • "There are similar threads on stackoverflow but none seem to have a solid fix/guide to solve this." Really? When I [try looking using the most obvious query I can think of](https://stackoverflow.com/search?q=%5Bpython%5D+no+module+named), all the top results I see ([example 1](https://stackoverflow.com/questions/17309288/importerror-no-module-named-requests) [example 2](https://stackoverflow.com/questions/25905540/importerror-no-module-named-tkinter) [example 3](https://stackoverflow.com/questions/7446187/no-module-named-pkg-resources)) have an accepted answer with clear steps. – Karl Knechtel Dec 12 '21 at 07:48
  • When I [specifically search about `pynput`](https://stackoverflow.com/search?q=no+module+named+pynput), I see specific results there, too. I don't understand why *none* of these results constitute "a solid fix/guide" for you. Did you try any of them? Exactly what did you do in order to try them? Exactly what happened as a result? – Karl Knechtel Dec 12 '21 at 07:50

4 Answers4

0

Make sure that you did install pynput:

$ python3 -m pip install pynput

And config the Python interpreter in Pycharm correctly, to the global python3 or your specific venv.

0

A way that worked for me is installing pynput directing from inside PyCharm by using the terminal found on the bottom right.

Inside the PyCharm terminal type:

pip3 install pynput

Note: This will only work if you already have Python3 installed on your system. Hope this helps! (:

enter image description here

Arin
  • 155
  • 1
  • 7
0

I was stuck on a similar issues for almost two days. I was new to using python so I didn't know how to use the IDE well. All that I was missing was checking off "inherit global site-packages".

After that, it worked fine

maac
  • 1
  • 1
0

I got this problem today and want to share my solution.

In my case I'm using virtual environment and I re-used the venv/ directory used in my last project, it was venv39/ and I renamed it to venv/, and copy/paste to new project, the module pytest was already in it.

Then I did pip install pynput, it looked pretty good when I used vscode or PyCharm to wrote the code, no module missing prompt from the IDE, but, when I ran pytest there's always no module named pynput.

Finally I guess the pytest.exe might be using the old python.exe or something like that, so I did pip uninstall pytest and pip install pytest, then the problem was gone.

Hope it help if you got the same case.

Reed_Xia
  • 1,300
  • 3
  • 17
  • 29