pythonnumpyflask

ModuleNotFoundError: No module named 'numpy' error in Flask app even if numpy present.


I'm developing a flask application in which I want to use numpy. I used import numpy as np but when I run the application I get this error, ModuleNotFoundError: No module named 'numpy'. I do have numpy install in python and also cross-checked it with importing numpy in python cmd as below:

C:\Users\Zirak Mistry>python
Python 3.7.1 (v3.7.1:260ec2c36a, Oct 20 2018, 14:05:16) [MSC v.1915 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
>>>

No error after executing the command import numpy means that numpy is installed in python and is working properly. Yet when I'm importing numpy into my flask application, I'm getting errors. Why so? please help.


Solution

  • Probably the Python you are using to run Flask is not the same as the one you tested the import numpy with, and numpy is installed in the wrong Python.

    You can use sys.executable to determine where you are running Python from. Try both in your Flask app and in cmd to see if they are the same.

    If they really are the same, it's also possible that the Flask app has changed where it is searching for modules. This can be changed programmatically. If it's not on sys.path the normal import mechanism won't find it.