Spyder refuses to acknowledge the existence of the keyboard module

I’ve been trying to import the keyboard module using Spyder. I’ve installed the module properly from the terminal, and running “python3 -m pip list” in the terminal says that I do have the module installed. I’m running Spyder (the latest version, which I believe is 5.1.5) in a python 3.9 environment, I checked my installed versions of python and pip in the terminal using “python --version” and “pip --version” and they both show up as python 3.9.

However, Spyder simply refuses to acknowledge that the keyboard module exists and I get this error:

ModuleNotFoundError: No module named ‘keyboard’

I’m at a complete loss, I have no idea what else I could do or check.

At the terminal, run

which python

to see what Python interpreter you are using when you installed the
keyboard module.

Then, from Spyder, run:

import sys
print(sys.executable)

to see which Python interpreter you are using in Spyder. My guess is
that you have two (or more!) Pythons installed.

You can also try this from the terminal:

python -c "import keyboard; print(keyboard.__file__)"

to see if the keyboard module really exists, or if pip is confused.

running “which python” gives me

'which' is not recognized as an internal or external command, operable program or batch file.

Printing sys.executable gave me this result

D:\Anaconda\Anaconda\envs\py39\python.exe

The last command you gave me returned this

C:\msys64\mingw64\lib\python3.9\site-packages\keyboard\__init__.py

which I assume means that the keyboard module does exist.

try “where python”

Okay, that gave me

C:\msys64\mingw64\bin\python.exe
C:\Users\hp\AppData\Local\Microsoft\WindowsApps\python.exe

So it does look like I have two installations of Python. Should I get rid of one of them? If so, how do I do that?

There is no need to get rid of the other versions. Spyder uses the Python version in Anaconda. To install packages, open the Anaconda Prompt, and activate the environment:

conda activate py39

Then install your package with conda

conda install keyboard

Okay, running that second command gave me a bunch of errors, but redirected me to the anaconda website where I found some instructions that helped me fix it. Thanks everyone!

Spyder core dev here. Spyder can be installed either via our standalone installers, or through Anaconda/Miniconda, pip, WinPython, Fink/MacPorts, your Linux distro, GitHub or other methods, and can use both the Python environment in which it is installed, and any other on your machine (Anaconda or not), so long as Spyder-Kernels is installed in that environment. You can change what environment that is under Preferences > Python Interpreter; it will autodetect Anaconda and virtualenvwrapper envs and you can manually enter the path to others (the one you found under sys.executable). We have some high-level FAQ questions that address this is moderate detail, and I have my official detailed guide on the topic; I’ve updated it yet again just now, though some parts are a bit dated as things are easier in modern Spyder.