Forums

ImportError: No module named flask

I have a flask app that was working fine for years. But I just got this error today:

Error running WSGI application ImportError: No module named flask File "/var/www/[user]_pythonanywhere_com_wsgi.py", line 16, in <module> from flask_app import app as application File "/home/[user]/mysite/flask_app.py", line 1, in <module> from flask import Flask,redirect

How should I fix this?

I have exactly the same problem. Do you have a fix?

2022-10-29 00:55:42,668: Error running WSGI application
2022-10-29 00:55:42,672: ModuleNotFoundError: No module named 'flask_cors'
2022-10-29 00:55:42,672:   File "/var/www/vinasia_pythonanywhere_com_wsgi.py", line 16, in <module>
2022-10-29 00:55:42,672:     from flask_app import app as application
2022-10-29 00:55:42,672: 
2022-10-29 00:55:42,672:   File "/home/vinasia/mysite/flask_app/__init__.py", line 5, in <module>
2022-10-29 00:55:42,672:     from flask_cors import CORS

No. I messaged pythonanywhere. When they reply or if I found a fix, I'll update.

On 27 October, your account was updated from our old "classic" system image, which has various Python packages installed for Python 2.7 and 3.5, to our newer "glastonbury" image, which doesn't have those pre-installed packages. If you run

pip3.5 install --user flask

...in a bash console (assuming that you're using Python 3.5 -- change the numbers if you're using a different Python version) then that will fix this issue.

BTW, for clarity -- over the last year, we've been sending emails to the owners of all accounts that were going to be affected by this change so that people would have a chance to make any required changes up-front and avoid any downtime.

I did a pip install for flask when I first had that error. I just ran it again to double check. It seems that flask is already installed. The message is below. Is the code in mysite running through a different python version?

16:47 ~ $ 
16:47 ~ $ python --version
Python 3.9.5
16:47 ~ $ pip3.9 install --user flask
Looking in links: /usr/share/pip-wheels
Requirement already satisfied: flask in /usr/local/lib/python3.9/site-packages (2.0.0)
Requirement already satisfied: itsdangerous>=2.0 in /usr/local/lib/python3.9/site-packages (from flask) (2.0.1)
Requirement already satisfied: click>=7.1.2 in /usr/local/lib/python3.9/site-packages (from flask) (7.1.2)
Requirement already satisfied: Jinja2>=3.0 in /usr/local/lib/python3.9/site-packages (from flask) (3.0.1)
Requirement already satisfied: Werkzeug>=2.0 in /usr/local/lib/python3.9/site-packages (from flask) (2.0.1)
Requirement already satisfied: MarkupSafe>=2.0 in /usr/local/lib/python3.9/site-packages (from Jinja2>=3.0->flask)

(2.0.1)

It's installed for python 3.9 but is your web app running on that python version?

I have already checked that. I tested it by adding the following to the top of flask_.app.py before the import that causes the error:

import os
os.system('python --version > stdout.txt')

stdout.txt (in my home directory) does have "Python 3.9.5". The only thing I can think of is the app is running "somewhere else" with python 3.9, but without flask installed? But then I have also done a "pwd" in the flask app and redirect just like the above and "pwd" says I'm in my home directory.

Do you see it in your web app's log or when you run your code in the console? Check the python version in your web app configuration on the "Web" page.