No module named 'pynput'

Question:

I am completely new to Python and and still in my babysteps at coding and can’t get this thing to work.

I am trying to build an auto-clicker as a learning experience, so I use pynput:

from pynput.mouse import Button, Controller
from pynput.keyboard import Listener, KeyCode

But I get the error:

    from pynput.mouse import Button, Controller
ModuleNotFoundError: No module named 'pynput'

As troubleshooting I again typed in the cmd “pip install pynput” and got:

Requirement already satisfied: pynput in c:program files (x86)pythonpython37-32libsite-packages (1.4)
Requirement already satisfied: six in c:program files (x86)pythonpython37-32libsite-packages (from pynput) (1.12.0)

Just to be sure, I also tried “pip3 install pynput” with the same result. When I am in the IDLE and type in “import pynput”, I get no errors. I only have one python version installed.

Do you have any ideas what I am still doing wrong?

If you need any more information, just let me know.

Thank you in advance.
JM

Asked By: JMP

||

Answers:

There might be one of these possibilities to this problem:

  • The package was not correctly installed. Uninstall it and install it again and see if issue persists.
  • There could be permission issue on the path where the package is installed. Does it have full rw permissions so python can access it? If you are using linux, use “sudo pip install”

  • If you have installed the package inside a virtualenv and running the program outside the virtualenv, the package will not be available.

Answered By: Chintan Bhatt

You should check the Interpreter the PyCharm uses for your project here:
File -> Settings -> Project: %Project_name% -> Project Interpreter.
It should be same as where you installed pynput.

Answered By: Alex B

You probably have multiple python installations and the one used by pycharm is not the one linked with the pip binary.

To solve this issue is it enough to install the library using pip as a module.

Step 1: understand what python interpreter you are actually using

import sys
print(sys.executable)

the output is your path_interpreter (something like /Users/xyz/bin/python)

Sept 2: run pip with that interpreter

from terminal: path_interpreter -m pip install pynput

That’s it.

UPDATE: if you get failed to acquire X connection: No module named 'tkinter', try sudo apt-get install python3-tk

Answered By: alec_djinn

I had a same problem with pynput module.
I fixed my problem in the below.

I checked my python file name and it was a “pynput.py”
This may call my file as pynput module.
So, I changed my file name “pynput.py” –> “pynput1.py”
And, it works well!!
I really hope it can resolve your problem

Answered By: Mr.Energizer

If your using PyCharm, try going to the terminal shell (that’s built in PyCharm), and type pip install pynput.

If you are using any different IDE, go to your device terminal and type the same thing.

Answered By: Hyperninja303
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.