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

3

18

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?

Slab answered 18/8, 2013 at 8:10 Comment(2)
This doesn't answer your question, but once you are in a virtual environment, you don't need to provide the path to the virtualized python; part of the activate.bat script is setting up your shell variables to point to the virtualized versions of things like python.exe and pip.exe. Also, you shouldn't put the virtual environment inside your source code directory (as it appears you are doing). The best practice is to put all virtualenvironments in their own separate directory and use the activate scripts to adjust your shell.Selfregulating
I'm not putting source in the virtualenv folder. It's in the app folder. I'm actually not using the scripts to install but running pycharm so I don't have to activate anything while changing project. It's handling all that automagically for me. Thanks anyway though!Slab
P
39

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
Prussia answered 18/8, 2013 at 8:24 Comment(1)
It's deprecated now, so you can use StringField instead. from wtforms import StringFieldCandlepower
C
10

I had the same problem and solve after i installed WTForms 2.3.3

pip install WTForms==2.3.3

from flask_wtf import FlaskForm
from flask_wtf import Form
from wtforms import TextField, BooleanField, TextAreaField
Countermeasure answered 11/11, 2021 at 16:26 Comment(0)
H
5

I think TextField is deprecated use StringField instead

from wtforms import StringField
Hubert answered 10/5, 2022 at 17:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.