Ubuntu16.04(Python2.7)下已装gevent仍报‘no module named gevent’求助
Hey there, let's work through this gevent import issue together—it's super common when dealing with multiple Python environments on Ubuntu, so let's break down the most likely fixes step by step:
Step 1: Verify your Python and pip environment
First, make sure you're using the right Python and pip versions for Python 2.7. Ubuntu 16.04 ships with both Python 2.7 and 3.x, so it's easy to mix them up:
- Check which Python you're running:
This should return something likewhich python/usr/bin/python(the system Python 2.7). If it returns a different path (like/usr/bin/python3), you're accidentally using Python 3. - Check which pip is associated with Python 2.7:
Usewhich pip2pip2explicitly for Python 2.7 packages instead of plainpip(which might default to pip3).
Step 2: Confirm gevent's installation path
Check where gevent is installed and if it's in Python 2.7's module search path:
- Run this to see gevent's install location:
pip2 show gevent - Then check Python 2.7's module search paths:
If the gevent path frompython -c "import sys; print('\n'.join(sys.path))"pip2 showisn't in this list, that's the core problem—Python can't locate the module.
Step 3: Reinstall gevent with the correct pip
If you previously mixed up apt-get or used the wrong pip, clean up and reinstall properly:
- Uninstall any existing versions first:
sudo apt-get remove --purge python-gevent pip2 uninstall gevent - Reinstall using pip2 (the Python 2.7-specific pip):
sudo pip2 install gevent - Test the import right away to confirm:
python -c "import gevent; print('Gevent imported successfully! Version:', gevent.__version__)"
Step 4: Check for virtual environment conflicts
If you're using a virtual environment (like virtualenv), make sure:
- You're activating the correct environment before running your program:
source /path/to/your/venv/bin/activate - You installed gevent inside the activated virtual environment (not globally):
pip install gevent # Run this after activating the venv
Step 5: Fix permission or path issues
If gevent is installed in your user directory (e.g., ~/.local/lib/python2.7/site-packages) but your program runs with a different user (or the path isn't in sys.path):
- Either add the path to your Python program temporarily:
import sys sys.path.append('/home/your_username/.local/lib/python2.7/site-packages') import gevent - Or install gevent globally with sudo to ensure it's in the system Python path:
sudo pip2 install gevent
内容的提问来源于stack exchange,提问作者DJ_Stuffy_K

