'Flask with Matplotlib (or Numpy, or Plotly) - ModuleNotFound
I'm trying to experiment with matplotlib charts in my SQL-backed flask app but I get the ModuleNotFound error when trying to import it. Note I also get the same errors when trying to import numpy or plotly express. My venv is able to recognize the SQLAlchemy library though, so I'm not sure what's going on. Any tips would be appreciated.
Minimal steps to reproduce:
mkdir flask_test
cd flask_test
python3 -m venv venv
. venv/bin/activate
Quick check of pre-install with pip list
Package Version
---------- -------
pip 21.3.1
setuptools 60.5.0
WARNING: You are using pip version 21.3.1; however, version 22.0.4 is available.
You should consider upgrading via the '/Users/<me>/Desktop/flask_test/venv/bin/python3.9 -m pip install --upgrade pip' command.
Go ahead and install:
pip install Flask SQLAlchemy matplotlib
Using cached Flask-2.1.1-py3-none-any.whl (95 kB)
Collecting SQLAlchemy
Using cached SQLAlchemy-1.4.35-cp39-cp39-macosx_10_15_x86_64.whl (1.5 MB)
Collecting matplotlib
Using cached matplotlib-3.5.1-cp39-cp39-macosx_10_9_x86_64.whl (7.3 MB)
Collecting importlib-metadata>=3.6.0
Using cached importlib_metadata-4.11.3-py3-none-any.whl (18 kB)
Collecting Werkzeug>=2.0
Using cached Werkzeug-2.1.1-py3-none-any.whl (224 kB)
Collecting click>=8.0
Using cached click-8.1.2-py3-none-any.whl (96 kB)
Collecting Jinja2>=3.0
Using cached Jinja2-3.1.1-py3-none-any.whl (132 kB)
Collecting itsdangerous>=2.0
Using cached itsdangerous-2.1.2-py3-none-any.whl (15 kB)
Collecting greenlet!=0.4.17
Using cached greenlet-1.1.2-cp39-cp39-macosx_10_14_x86_64.whl (92 kB)
Collecting packaging>=20.0
Using cached packaging-21.3-py3-none-any.whl (40 kB)
Collecting python-dateutil>=2.7
Using cached python_dateutil-2.8.2-py2.py3-none-any.whl (247 kB)
Collecting pillow>=6.2.0
Using cached Pillow-9.1.0-cp39-cp39-macosx_10_9_x86_64.whl (3.1 MB)
Collecting pyparsing>=2.2.1
Using cached pyparsing-3.0.8-py3-none-any.whl (98 kB)
Collecting numpy>=1.17
Using cached numpy-1.22.3-cp39-cp39-macosx_10_14_x86_64.whl (17.6 MB)
Collecting kiwisolver>=1.0.1
Using cached kiwisolver-1.4.2-cp39-cp39-macosx_10_9_x86_64.whl (65 kB)
Collecting cycler>=0.10
Using cached cycler-0.11.0-py3-none-any.whl (6.4 kB)
Collecting fonttools>=4.22.0
Using cached fonttools-4.32.0-py3-none-any.whl (900 kB)
Collecting zipp>=0.5
Using cached zipp-3.8.0-py3-none-any.whl (5.4 kB)
Collecting MarkupSafe>=2.0
Using cached MarkupSafe-2.1.1-cp39-cp39-macosx_10_9_x86_64.whl (13 kB)
Collecting six>=1.5
Using cached six-1.16.0-py2.py3-none-any.whl (11 kB)
Installing collected packages: zipp, Werkzeug, six, pyparsing, pillow, numpy, MarkupSafe, kiwisolver, itsdangerous, greenlet, fonttools, cycler, click, SQLAlchemy, python-dateutil, packaging, Jinja2, importlib-metadata, matplotlib, Flask
Successfully installed Flask-2.1.1 Jinja2-3.1.1 MarkupSafe-2.1.1 SQLAlchemy-1.4.35 Werkzeug-2.1.1 click-8.1.2 cycler-0.11.0 fonttools-4.32.0 greenlet-1.1.2 importlib-metadata-4.11.3 itsdangerous-2.1.2 kiwisolver-1.4.2 matplotlib-3.5.1 numpy-1.22.3 packaging-21.3 pillow-9.1.0 pyparsing-3.0.8 python-dateutil-2.8.2 six-1.16.0 zipp-3.8.0
Exit out of venv shell
deactivate
Create the hello world app (hello.py) per Flask quickstart:
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello_world():
return "<p>Hello, World!</p>"
Run it
export FLASK_APP=hello
export FLASK_ENV=development
flask run
It runs fine and I can see hello world
* Serving Flask app 'hello' (lazy loading)
* Environment: development
* Debug mode: on
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger PIN: 687-225-XXX
Now I try import SQLAlchemy:
from flask import Flask
from sqlalchemy import inspect
app = Flask(__name__)
@app.route("/")
def hello_world():
print(inspect)
return "<p>Hello, World!</p>"
I can see the inspect object being printed in the console:
<function inspect at 0x10e999ee0>
Now try import matplotlib:
from flask import Flask
from sqlalchemy import inspect
import matplotlib.pyplot as plt
app = Flask(__name__)
@app.route("/")
def hello_world():
print(inspect)
print(plt)
return "<p>Hello, World!</p>"
When I get this error:
flask.cli.NoAppException: While importing 'hello', an ImportError was raised:
Traceback (most recent call last):
File "/usr/local/lib/python3.9/site-packages/flask/cli.py", line 260, in locate_app
__import__(module_name)
File "/Users/<me>/Desktop/flask_test/hello.py", line 3, in <module>
import matplotlib.pyplot as plt
ModuleNotFoundError: No module named 'matplotlib'
Solution 1:[1]
For the sake of future reference:
It seems as if both Flask and SqlAlchemy were installed system level thus giving the false impression that something went wrong with the matplotlib install.
There are a few options:
Start the virtualenv and then run flask. i.e.
. ./venv/bin/activate FLASK_APP=hello FLASK_ENV=development flask runRun the flask binary directly i.e.
FLASK_APP=hello FLASK_ENV=development ./venv/bin/flask runInstall matplotlib system level (Not really recommended, just adding this for completeness)
Since you already created your virtualenv, use Option #1.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | ewong |
