ImportError: No module named flask_wtf

Viewed 36480

I am getting the error, ImportError: No module named flask_wtf. I tried every possible solution. I installed flask-wrf using -

$ flask/bin/pip install flask-wtf

Requirement already satisfied: flask-wtf in ./flask/lib/python2.7/site-packages
Requirement already satisfied: WTForms in ./flask/lib/python2.7/site-packages.

Please help me out with this issue.

I am sharing my code which shows error:

from flask_wtf import Form 
from wtforms import StringField, BooleanField
from wtforms.validators import DataRequired
    
class LoginForm(Form):
    openid = StringField('openid', validators=[DataRequired()])
    remember_me = BooleanField('remember_me', default=False)
13 Answers

I also have same issue. But installing Flask-WTF

pip install flask-wtf

I was able to resolve this issue.

This is certainly a problem with virtual environment. I had the same problem and managed to get rid of errors with sudo pip install Flask-WTF while my virtual environment was active.

This solution, however, has got me into another package (email_validator) importing errors, though I was sure everything is installed as it should be. Now, instead of running flask run from terminal, I decided to run the whole application as it, by typing python3 application.py in my app directory, where application.py is my flask app.

In this way of running application I haven't come into any errors at all. If you want to run your app like me, be sure to set following conditional at the bottom of your application:

if __name__ == '__main__':
    app.run(debug=True)

(This will run your application in debug mode.)

I'm beginner level developer. It would be really nice for someone more experienced to explain logic behind this.

Edit: Once I ran my application like this, suddenly I can run it with flask run again, without any errors. Interesting.

Installing Flask-WTF is simple with pip:

pip install Flask-WTF

In my case it worked after I installed it using pip3 instead of just pip. I didn't look into it enough to find out what was different but I'm seriously thinking of linking pip to pip3 in my venv.

I've had the same problem. The problem in my case was that Flask was installed globally , so flask run called the global package that didn't recognise the local package (Probably because the global call to python called python2, and my env was set to python3) . I solved it by uninstalling Flask globally and installing it only in the virtual environment.

I had the same problem Solved it using following

conda install -c wakari flask-wtf

Note that if you are using Anaconda's python to run your app then you have to pip install flask-wtf from the Anaconda prompt and not from general command prompt. This helped me when I encountered this problem. Hope this helps!

You could install flask_wtf in conda using this command

conda install -c anaconda flask-wtf

Since your problem is due to virtualenv, installing this on conda will solve the problem. It did work for me.

In my case it was the python version, i tried all of the above proposition, i changed the python version from 3 to 2, and it work perfectly.

python2 app.py

Extending the @pawelduzy's answer adding a app.run(debug=True) resolved the same issue in my virtual environement where Flask-WTF extension was already installed via pip install flask-wtf.

This is the content of my '__init__.py' file

from flask import Flask
from config import Config

app = Flask(__name__)
app.config.from_object(Config)
app.run(debug=True)

from app import routes

Alternatively you may insert the app.run(debug=True) statement into a .py-file that launches the main application

from app import app
app.run(debug=True)

But be aware of what is mentioned in the Flask's Docs, namely "Debugging Application Errors"

Having problems getting your application configured for production? If you have shell access to your host, verify that you can run your application manually from the shell in the deployment environment. Be sure to run under the same user account as the configured deployment to troubleshoot permission issues. You can use Flask’s builtin development server with debug=True on your production host, which is helpful in catching configuration issues, but be sure to do this temporarily in a controlled environment. Do not run in production with debug=True.

Had the same error "ModuleNotFoundError: No module named 'flask_wtf'". This worked for me:

  1. Open Anaconda - install flask_wtf in using this command in the Anaconda terminal:

    conda install -c anaconda flask-wtf
    
  2. In Pycharm go to preferences/project/python interpreter. Click the "+" button on the lower left then search for "flask-wtf. Then install the package.

The second step was not possible without the first, since flask-wtf didn't show up in the list of available packages.

Related