You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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’s PyCharm > 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 > Add to 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 for scipy, and install it directly from here.

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:

  1. Open PyCharm’s built-in terminal (bottom toolbar, click Terminal)
  2. Activate your project’s environment:
    • For Linux/macOS: source venv/bin/activate (replace venv with your virtual environment folder name)
    • For Windows: venv\Scripts\activate
  3. Install the IPython kernel package if you haven’t already:
    pip install ipykernel
    
  4. Register your environment as a Jupyter kernel:
    python -m ipykernel install --user --name=my-project-env
    
    (Replace my-project-env with a name you’ll recognize, like your project’s name)
  5. 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).
  6. 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) or where python (Windows), then which pip/where pip. The paths should match (e.g., both pointing to your virtual environment’s bin or Scripts folder). If they don’t, use python -m pip install --upgrade --force-reinstall scipy instead of just pip—this ensures you’re installing to the active Python interpreter.
  • Verify scipy in Jupyter: In a code cell of your .ipynb file, run:
    !pip list | grep scipy
    
    If scipy doesn’t show up, run !pip install scipy directly 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 07:38:58