树莓派Debian系统virtualenv中pip安装scikit-learn失败
Hey there, let's get your scikit-learn installed properly in your virtualenv. Looking at your error logs, the key issue is ImportError: No module named 'Cython'—this happens because installing scikit-learn from source (which occurs when precompiled wheels aren't automatically used) requires Cython as a build dependency. Plus, compiling on Raspberry Pi can be slow and error-prone, so we'll use precompiled wheels from piwheels to make this smoother.
Here's the step-by-step fix:
1. Ensure your virtualenv is activated
First, make sure you're working inside your target virtualenv. Run the activation command:
source /home/pi/python_virtual_env/neural_networks/bin/activate
You'll see the virtualenv name (neural_networks) in your terminal prompt once activated.
2. Install build dependencies
Before installing scikit-learn, install the required build tools and Cython directly in your virtualenv:
pip install cython gcc gfortran
cython: Fixes the missing module error from your logs.gccandgfortran: Needed for compiling any remaining native code (though piwheels will minimize this work).
3. Install scikit-learn using piwheels
Your log already shows piwheels is in your package index list, so we'll explicitly use it to grab the precompiled ARM wheel (no more slow compilation!):
pip install --only-binary :all: scikit-learn==0.21.2
We specify version 0.21.2 since that's the one your log tried to install (it's compatible with Python 3.5). If you want a newer compatible version, you can check piwheels for available options, but 0.21.2 is a safe, tested choice.
4. Verify the installation
Once the install finishes, test importing sklearn in Python to confirm success:
python -c "import sklearn; print(sklearn.__version__)"
You should see 0.21.2 printed without any errors.
Why your previous attempts failed:
- Using
sudo pip3 install scikit-learninstalls to the system Python 3 environment, not your virtualenv—so it doesn't help your targeted setup. - Without Cython in the virtualenv, the source compilation of scikit-learn fails immediately, even though OpenBLAS was detected.
- Using piwheels avoids compiling from source entirely, which is far more reliable on Raspberry Pi's ARM architecture.
内容的提问来源于stack exchange,提问作者Sebastian Bloy

