部署Django应用时遇ModuleNotFoundError: No module named 'scipy._lib'求助
Hey there, let's tackle this ModuleNotFoundError: No module named 'scipy._lib' issue you're facing on PythonAnywhere. I've seen similar problems with older scipy versions and virtual environment mismatches, so here are some targeted steps to fix it:
Double-check your virtual environment is activated
It's easy to accidentally run your Django server outside the virtual environment where you installed scipy, which would use PythonAnywhere's system Python instead of your custom libraries.- Activate your virtual environment first:
# If you used virtualenvwrapper workon your_virtual_env_name # Or if you created it with plain virtualenv source /path/to/your/virtualenv/bin/activate - After activating, run
pip3 listagain to confirm scipy 1.1.0 is listed under this environment, then trypython3 manage.py runserveronce more.
- Activate your virtual environment first:
Reinstall scipy to fix incomplete installation
Sometimes scipy installs can be partial due to network blips or missing system dependencies. Let's do a clean reinstall:# Uninstall existing scipy completely pip3 uninstall -y scipy # Reinstall the exact version you need pip3 install scipy==1.1.0If this still fails, try upgrading to a slightly newer version that's more compatible with Ubuntu 14.04 (scipy 1.2.3 works reliably with Python 3.4-3.6):
pip3 install scipy==1.2.3Verify Python version consistency
Older scipy versions like 1.1.0 have strict Python version compatibility. Make sure the Python version in your PythonAnywhere virtual environment matches your local machine:- Check your local Python version:
python3 --version - Check PythonAnywhere's virtual environment version after activation:
python3 --version
If they don't match, create a new virtual environment with the correct Python version (e.g., for Python 3.6 on PythonAnywhere):
mkvirtualenv --python=/usr/bin/python3.6 your_new_envThen reinstall all your dependencies including scipy.
- Check your local Python version:
Install required system libraries
Scipy relies on low-level math libraries that might be missing on Ubuntu 14.04. Run these commands in the PythonAnywhere console to install them:sudo apt-get update sudo apt-get install libblas-dev liblapack-dev gfortranAfter installing these system dependencies, reinstall scipy as mentioned earlier.
Validate scipy's installation path
Ensure scipy is installed in your virtual environment's site-packages directory:- Activate your env, then run:
Look for thepip3 show scipyLocationfield—it should point to something like/home/your_username/.virtualenvs/your_env/lib/pythonX.X/site-packages - Confirm this path is in Python's system path:
python3 -c "import sys; print('\n'.join(sys.path))"
If the site-packages path isn't listed, your virtual environment activation might be broken. Try recreating the virtual environment from scratch.
- Activate your env, then run:
内容的提问来源于stack exchange,提问作者Kirill Korolev

