I'm not able to import Flask-WTF TextField and BooleanField

Asken picture Asken · Aug 18, 2013 · Viewed 16.9k times · Source

I'm using virtualenv to set up a new project. I installed a lot of things using virtualenv pip from the script folder like below:

flask\scripts\pip install Flask-WTF

I have no other packages installed in the global python folder. My code looks like this:

# Importing TextField and BooleanField is not working...
from flask.ext.wtf import Form, TextField, BooleanField
from flask.ext.wtf import Required


class LoginForm(Form):
    openid = TextField('openid', validators=[Required()])
    remember_me = BooleanField('remember_me', default=False)

and other packages are found like sqlalchemy also installed only in the virtual environment.

The error I get is:

(flask) D:\Development\grading>flask\Scripts\python.exe restserver.py Traceback (most recent call last):
File "restserver.py", line 1, in <module> from app import app
File "D:\Development\grading\app\__init__.py", line 12, in <module> from forms import LoginForm
File "D:\Development\grading\app\forms.py", line 1, in <module> from flask.ext.wtf import Form, TextField, BooleanField
File "D:\Development\grading\flask\lib\site-packages\flask\exthook.py", line 87, in load_module
raise ImportError('No module named %s' % fullname) ImportError: No module named flask.ext.wtf.TextField

Form is found but not TextField and BooleanField. What is the problem here?

Update I just looked through some of the Flask-WTF code and found this:

from flask.ext.wtf import Form
from wtforms.fields import TextField, BooleanField
from wtforms.validators import Required

Am I using examples from an older version or something?

Answer

Blender picture Blender · Aug 18, 2013

From version 0.9.0, Flask-WTF will not import anything from wtforms, you need to import fields from wtforms.

Source

You need to import them from wtforms (note that according to docs import statement was changed):

from flask_wtf import Form

from wtforms import TextField, BooleanField
from wtforms.validators import Required