Python新手求助:已安装pypresence仍触发ModuleNotFoundError
ModuleNotFoundError: No module named 'pypresence' Even After Installation Hey there! I get it, it's super frustrating when you install a package like you're supposed to, but your code still throws a module not found error. Let's break down the most common reasons this happens and how to fix them step by step.
First, let's recap your code for reference (I cleaned it up a bit for readability):
from pypresence import Presence import time client_id = '...' # Don't forget to replace this with your actual Discord client ID! RPC = Presence(client_id) RPC.connect() print(RPC.update( state="Lookie Lookie", details="A test of qwertyquerty's Python Discord RPC wrapper, pypresence!" )) while True: time.sleep(15)
Common Causes & Solutions
1. You're using a different Python version than the one you installed pypresence to
This is by far the most common issue. If your system has multiple Python versions (like Python 2 and 3), pip might be installing the package for one version, but you're running your code with another.
- Fix: Use version-specific
pipandpythoncommands:- Install with
pip3 install pypresence(for Python 3, which is the version you should be using) - Run your script with
python3 your_script_name.py
- Install with
To check which Python version you're using, run python --version or python3 --version in your terminal.
2. You're working in a virtual environment, but installed the package globally
If you set up a virtual environment for your project (like venv or conda), packages installed with regular pip go to your global Python installation—not the virtual environment your code is running in.
- Fix:
- Activate your virtual environment first:
- On Windows:
venv\Scripts\activate - On macOS/Linux:
source venv/bin/activate
- On Windows:
- Then run
pip install pypresenceagain while the environment is active.
- Activate your virtual environment first:
3. The package isn't installed in a directory Python checks for modules
Sometimes, pip installs packages to a location that isn't in your Python's module search path. Let's verify this:
First, check where pypresence is installed:
pip show pypresenceLook for the
Locationline in the output.Then, check which directories Python looks for modules in:
import sys print(sys.path)Run this in the same Python environment you're using to run your script.
If the Location from pip show isn't in the sys.path list, try these fixes:
- Reinstall the package using the
--userflag (installs to your user directory, which is almost always insys.path):pip install --user pypresence - Or, manually add the installation directory to
sys.pathat the top of your script (a quick, temporary fix):import sys sys.path.append("/path/to/pypresence/location") # Replace with your actual path from pip show from pypresence import Presence # Rest of your code...
4. Permission issues (Windows/macOS/Linux)
On some systems, you might not have permission to install packages to the global Python directory. Using the --user flag (mentioned above) fixes this by installing to your user-specific directory instead of the system-wide one.
For Linux/macOS, avoid using sudo pip install unless absolutely necessary—it can mess up your system Python installation.
Once you try these steps, your script should be able to find the pypresence module. Double-check that you've replaced '...' with your actual Discord client ID too!
内容的提问来源于stack exchange,提问作者user14201863

