从sklearn.externals导入joblib失败及模型加载报错求助
Hey there, let's work through these two issues you're facing with your Flask ML prediction service—they're both typical when moving models between environments or updating libraries!
What's causing this?
Scikit-learn removed the sklearn.externals.joblib import path starting in version 0.23. If you're running a newer version of scikit-learn now (compared to 2 months ago), that import will fail because it's no longer supported.
Fix:
Replace:
from sklearn.externals import joblib
With:
import joblib
Make sure you have the joblib package installed in your environment—if not, run:
pip install joblib
What's causing this?
This error almost always stems from version mismatches between the environment where you saved the classifier.joblib model and your current environment. Specifically:
- The scikit-learn version you used to save the model is different from the one you're using now
- The joblib version is incompatible with either the saved model or your current scikit-learn version
- Rarely, a mismatch between 32-bit and 64-bit system architectures can trigger this too
Fixes to try:
Match the exact library versions from when you saved the model
If you remember the scikit-learn and joblib versions used 2 months ago, install those exact versions:pip install scikit-learn==[your-old-version] joblib==[corresponding-joblib-version]Tip: If you don't remember the versions, check old
requirements.txtfiles or environment snapshots (like conda env exports) if you have them.Retrain and re-save the model in your current environment
If you can't access the old versions, the most reliable fix is to re-run your model training code in your current Flask environment, then save a newclassifier.joblibfile. This ensures the model is fully compatible with your current library versions.Verify system architecture consistency
Make sure your current environment is running on the same architecture (32-bit vs 64-bit) as the machine where you saved the model. Different architectures can cause dtype mismatches in serialized files.
内容的提问来源于stack exchange,提问作者Dilipan M

