无法导入pynput.keyboard模块求助:已pip安装仍报错
ImportError: No module named pynput.keyboard Issue Hey there, let's work through this pynput import problem together! It's really frustrating when pip says the module is installed but your code can't find it—here are the most common fixes to try, plus a note on your code's functionality:
1. Make sure you're installing pynput for the right Python interpreter
This is the #1 culprit for "module not found" errors after pip install. Here's how to check:
- On Linux/macOS, run
which pythonto see the path of the Python you're using to runsb.py, then runwhich pipto see which pip you're using. If the paths don't match, pip is installing to a different Python environment. - On Windows, use
where pythonandwhere pipto do the same check.
To fix this, install pynput directly for your target Python interpreter using:
python -m pip install pynput
If you're using Python 3 (and your code has Python 2 syntax, which we'll cover next), use python3 -m pip install pynput instead.
2. Install a Python 2-compatible version of pynput
Looking at your code, you're using Python 2 syntax (like print "Welcome to soundboard" instead of print("Welcome to soundboard")). The latest versions of pynput no longer support Python 2, so if you're sticking with Python 2, you need to install an older, compatible version:
pip install pynput==1.6.8
3. Check for virtual environment conflicts
If you're using a virtual environment, make sure you've activated it before installing pynput and running your code:
- Linux/macOS:
source your_venv/bin/activate - Windows:
your_venv\Scripts\activate
Once activated, re-run pip install pynput (or the Python 2-compatible version) and try running sb.py again.
4. Fix the functionality issues in your code
Even if you get the import working, your current code has a few problems that will cause errors:
keyisn't defined in thegame()function—you probably want to listen for key presses using pynput'sListenerclass, not callkey.press()directly.Sound()isn't defined either—you'll need an audio library likewinsound(Windows) orpygameto play your audio file.- In Python 2,
input()executes the user's input as code, which is unsafe—useraw_input()instead.
Here's a revised version of your code (for Python 2 + pynput 1.6.8) that works:
from pynput.keyboard import Listener import winsound def play_sound(): # Replace with the full path to your audio file (make sure it's a WAV file for winsound) winsound.PlaySound("Cymatics - Guitar Chord Loop 2 - 128 BPM Dmaj.wav", winsound.SND_FILENAME) def on_key_press(key): try: # Play sound when the 's' key is pressed if key.char == 's': play_sound() # Stop the program when 'stop' is typed elif key.char == 'stop': return False except AttributeError: # Handle special keys (like space, backspace) if needed pass def game(): print "Listening for keys... Press 's' to play the sound, type 'stop' to quit." with Listener(on_press=on_key_press) as listener: listener.join() print "Welcome to soundboard" print "You will see a list of sound names, just write the name of it" print "If you want to stop it type stop" print "Type start" start = raw_input() if start.lower() == "start": game() else: print "Oops, try again.."
Give these steps a shot—they should resolve the import error and get your soundboard working!
内容的提问来源于stack exchange,提问作者Viorel Andrei Chis




