14

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)
davidism
  • 121,510
  • 29
  • 395
  • 339
kanishka makhija
  • 155
  • 1
  • 1
  • 4
  • 2
    Are you using a virtualenv? Are you possibly installing `flask_wtf` globally instead of in a virtualenv? Also I think you need `from flask_wtf import FlaskForm` but that won't give you a module error – Cfreak May 23 '17 at 00:38
  • can you share full Traceback log? – Tiny.D May 23 '17 at 00:43
  • 1
    Try `from flask.ext.wtf import Form` – Mikael May 23 '17 at 03:13
  • 1
    yes i am using virtualenv and i also tried **from flask_wtf import FlaskForm** and i even also tried **from flask.ext.wtf import Form** – kanishka makhija May 23 '17 at 07:30
  • @Tiny.D Traceback (most recent call last): File "run.py", line 1, in from app import app File "/home/kanishka/projects/microblog/app/__init__.py", line 6, in from app import views File "/home/kanishka/projects/microblog/app/views.py", line 3, in from .forms import LoginForm File "/home/kanishka/projects/microblog/app/forms.py", line 1, in from flask_wtf import Form ImportError: No module named flask_wtf – kanishka makhija May 23 '17 at 10:03
  • yes i am using virtualenv – kanishka makhija May 23 '17 at 21:05

13 Answers13

17

I also have same issue. But installing Flask-WTF

pip install flask-wtf

I was able to resolve this issue.

davidism
  • 121,510
  • 29
  • 395
  • 339
GPrathap
  • 7,336
  • 7
  • 65
  • 83
5

Ensure you run the virtual environment's python rather than global python. I resolved this in my windows env through executing:

\inetpub\projectFolder\scripts\python.exe run.py

rather than just

python run.py

Also, make sure that you have installed the modules to the virtual environment rather than globally. Check that the modules exist appropriately - again under windows I checked for the file:

\inetpub\projectFolder\lib\site-packages\wtforms 
Piquet
  • 51
  • 1
  • 2
3

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.

pawelduzy
  • 41
  • 2
  • Just want to echo @pawelduzy with my experience. Running `python app.py` seemed to solve my ImportError after `flask run`. Not sure where the cause was but in case this might be helpful to someone. – Carl H Aug 29 '20 at 19:55
  • I tried all the other solutions but running python app_name.py was the only one that cleared up the error for me. – smilesr Oct 03 '20 at 10:01
2

Installing Flask-WTF is simple with pip:

pip install Flask-WTF
Joseph Cho
  • 4,033
  • 4
  • 26
  • 33
2

i did simple installation from flask_wtf and it worked the command is following

pip install Flask-WTF

please go to this link for more info https://flask-wtf.readthedocs.io/en/stable/install.html

2

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.

wprager
  • 21
  • 1
1

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.

Yotam Hacohen
  • 93
  • 3
  • 12
1

I had the same problem Solved it using following

conda install -c wakari flask-wtf
Tek Nath Acharya
  • 1,676
  • 2
  • 20
  • 35
Kumar Atul
  • 11
  • 1
0

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!

0

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.

BlackSwan
  • 275
  • 3
  • 12
0

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
jnovack
  • 7,629
  • 2
  • 26
  • 40
0

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.

Taras
  • 266
  • 6
  • 23
0

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.

k4th4
  • 21
  • 2