Python2.7 PyCharm虚拟环境安装requests后无法导入求助
Troubleshooting
requests Import Error in PyCharm Django Project Hey there! Let's work through this frustrating requests import issue together—it's almost always a misalignment between your virtual environment and PyCharm's configured interpreter. Here's what to check step by step:
1. Confirm PyCharm is using your activated virtual environment
This is the most common culprit. Let's verify:
- Go to
File > Settings > Project: [Your Project Name] > Python Interpreter(on Mac, it'sPyCharm > Settings > Project: [Your Project Name] > Python Interpreter) - Look through the list of installed packages. If
requestsisn't here, PyCharm is using a different environment than the one you installed it in. - To fix this: Click the gear icon in the top-right >
Add > Virtualenv Environment > Existing environment - Navigate to your virtual environment's Python executable:
- Windows:
venv\Scripts\python.exe - Mac/Linux:
venv/bin/python
- Windows:
- Select it, hit OK, and check if
requestsnow appears in the package list.
2. Install requests directly in PyCharm's Terminal
Sometimes the system terminal and PyCharm's terminal use different environments. Let's bypass that:
- Open the Terminal tab at the bottom of PyCharm
- Activate your virtual environment here:
- Windows:
venv\Scripts\activate - Mac/Linux:
source venv/bin/activate
- Windows:
- Run
pip install requests - Now try importing
requestsin your code again.
3. Clear PyCharm's cache and restart
IDE caches can sometimes hold onto old environment data. Let's reset that:
- Go to
File > Invalidate Caches... - Check the box for "Clear file system cache and local history"
- Click
Invalidate and Restart - Once PyCharm reopens, test the import again.
4. Verify your virtual environment works outside PyCharm
Let's rule out a broken virtual environment:
- Open your system terminal (not PyCharm's)
- Activate your virtual environment
- Run
pip list—you should seerequestslisted - Run
pythonto open a Python shell, then typeimport requests- If this works, the issue is definitely with PyCharm's configuration (go back to step 1)
- If this also throws an error, your virtual environment is corrupted. Delete it, create a new one with
python -m venv venv, activate it, and reinstall all your dependencies (includingrequestsand Django).
内容的提问来源于stack exchange,提问作者amazing carrot soup




