PyCharm虚拟机运行Keras版Jupyter Notebook时Scipy导入异常求助
Fixing "ImportError: No module named scipy" in Keras after Installing Scipy in PyCharm Jupyter
Hey there, I’ve dealt with this exact "environment mismatch" headache before—let’s get this sorted out step by step. The core issue here is almost always that the environment Keras is running in (either PyCharm’s project interpreter or your Jupyter kernel) isn’t the same one where you installed scipy. Here’s how to diagnose and fix it:
1. Verify PyCharm’s Project Interpreter
First, make sure PyCharm is using the same environment where you installed scipy:
- Go to
File > Settings > Project: [Your Project Name] > Python Interpreter(on macOS, it’sPyCharm > Settings > Project: [Your Project Name] > Python Interpreter) - Look at the interpreter listed at the top. Is this the virtual environment/conda environment where you ran
pip install scipy?- If not, click the gear icon >
Addto select the correct environment, or use the dropdown to switch to it. - If it is the right environment, scroll through the package list to confirm scipy is present. If not, click the
+button, search forscipy, and install it directly from here.
- If not, click the gear icon >
2. Fix Jupyter Kernel Environment Mismatch
Since you’re using a .ipynb file, PyCharm might be using a Jupyter kernel that’s separate from your project environment:
- Open PyCharm’s built-in terminal (bottom toolbar, click
Terminal) - Activate your project’s environment:
- For Linux/macOS:
source venv/bin/activate(replacevenvwith your virtual environment folder name) - For Windows:
venv\Scripts\activate
- For Linux/macOS:
- Install the IPython kernel package if you haven’t already:
pip install ipykernel - Register your environment as a Jupyter kernel:
(Replacepython -m ipykernel install --user --name=my-project-envmy-project-envwith a name you’ll recognize, like your project’s name) - Go back to your .ipynb file in PyCharm. Click the kernel selector in the top-right corner (it might say something like
Python 3.10), and select the kernel you just registered (my-project-env). - Restart the kernel (click the circular arrow icon next to the selector) and run your Keras code again.
3. Quick Checks to Rule Out Caching or Pip Misalignment
- Restart PyCharm: Sometimes the IDE caches old interpreter settings—closing and reopening it can refresh things.
- Check pip and Python alignment: In your terminal, run
which python(Linux/macOS) orwhere python(Windows), thenwhich pip/where pip. The paths should match (e.g., both pointing to your virtual environment’sbinorScriptsfolder). If they don’t, usepython -m pip install --upgrade --force-reinstall scipyinstead of justpip—this ensures you’re installing to the active Python interpreter. - Verify scipy in Jupyter: In a code cell of your .ipynb file, run:
If scipy doesn’t show up, run!pip list | grep scipy!pip install scipydirectly in the cell to install it to the kernel’s environment.
Why This Happens
When you run pip install scipy in a terminal, you’re installing it to whatever Python environment is active at that time. But if PyCharm’s project interpreter or Jupyter kernel is using a different environment (like the system Python or another virtual env), Keras won’t see the installed scipy. Aligning all three (terminal environment, PyCharm interpreter, Jupyter kernel) fixes the problem.
内容的提问来源于stack exchange,提问作者Sam Germain

