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

Windows下pip安装scikit-learn后无法导入且pip失效求助

Fixing "ModuleNotFoundError: No module named 'scipy._lib._util'" & Broken Pip After Installing Scikit-learn

Hey there, let's work through this issue step by step — it looks like two linked problems are happening here: a corrupted SciPy installation (triggering the sklearn import error) and a broken pip after running the upgrade command. Let's fix pip first, then clean up and reinstall the problematic libraries.

Step 1: Repair Your Broken Pip

Since pip is no longer recognized in CMD, we'll use Python's built-in ensurepip tool to restore it to working order:

  • Open Command Prompt and run:
    python -m ensurepip --upgrade
    
  • If that doesn't work (maybe your Python 3.9 isn't in the default system PATH), specify the version explicitly:
    py -3.9 -m ensurepip --upgrade
    

Verify pip is working by running pip --version — you should see its version number paired with your Python 3.9 installation path.

Step 2: Clean Up Corrupted Dependencies

The scipy._lib._util error comes from a partially installed or corrupted SciPy package. Let's remove all traces of scikit-learn and SciPy first to start fresh:

  1. Uninstall the libraries via pip:
    pip uninstall -y scikit-learn
    pip uninstall -y scipy
    
  2. Manually delete any leftover files (uninstallers sometimes miss these):
    • Navigate to C:\Users\Ryan\AppData\Local\Programs\Python\Python39\lib\site-packages
    • Delete any folders starting with sklearn, scipy, or scikit_learn
    • Also delete corresponding .dist-info folders (e.g., scipy.dist-info or scikit_learn.dist-info)

Step 3: Reinstall Libraries Safely

Since you're using Python 3.9, we'll pick library versions that are guaranteed to be compatible (scikit-learn 1.3+ no longer supports Python 3.9):

  1. First install a stable, compatible SciPy version:
    pip install scipy==1.8.1
    
  2. Then install a compatible scikit-learn version:
    pip install scikit-learn==1.2.2
    

Skip the -U (upgrade) flag for now — it’s what caused the dependency mess in the first place.

Step 4: Verify the Fix

Open a Python shell and test the imports to confirm everything works:

import sklearn
import scipy

If no errors pop up, you’re all set! Double-check pip still works by running pip list to see both libraries listed in your installed packages.

Pro Tips for New Python Users

  • Avoid blind upgrades: The -U flag upgrades all dependencies, which can break compatibility, especially with older Python versions like 3.9. Only use it if you specifically need the latest features.
  • Use virtual environments: Create isolated environments for each project with python -m venv myenv to avoid messing up your global Python setup. Activate the environment before installing libraries, and all changes stay contained to that project.
  • Check version compatibility: Always confirm the library versions you’re installing support your Python version. For Python 3.9, scikit-learn 1.2.x is the last supported series, and SciPy up to 1.10.x works reliably.

内容的提问来源于stack exchange,提问作者Glooc

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.11 08:20:16