Windows下pip安装scikit-learn后无法导入且pip失效求助
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:
- Uninstall the libraries via pip:
pip uninstall -y scikit-learn pip uninstall -y scipy - 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, orscikit_learn - Also delete corresponding
.dist-infofolders (e.g.,scipy.dist-infoorscikit_learn.dist-info)
- Navigate to
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):
- First install a stable, compatible SciPy version:
pip install scipy==1.8.1 - 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
-Uflag 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 myenvto 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

