WTForms: 安装’em ail_validator’以获得电子邮件验证支持

在运行以下表单验证代码时获取异常。

File "/Users/homeduvvuri/Documents/Learning/PartyGoUdemy/PartGo/user/forms.py", line 11, in BaseUserForm
email = EmailField('Email', [validators.DataRequired(), validators.Email()])
File "/Users/homeduvvuri/Documents/Learning/PartyGoUdemy/PartGo/partgo-env/lib/python3.7/site-packages/wtforms/validators.py", line 332, in __init__
raise Exception("Install 'email_validator' for email validation support.")
Exception: Install 'email_validator' for email validation support.

在 codewhere VM 上完美运行,在本地机器上则不然。

from flask_wtf import FlaskForm
from flask_wtf.file import FileField, FileAllowed
from wtforms import Form, StringField, PasswordField, validators, ValidationError
from wtforms.validators import InputRequired, Email
from wtforms.fields.html5 import EmailField
from wtforms.widgets import TextArea
from user.models import User


class BaseUserForm(FlaskForm):
name = StringField('Name', [validators.DataRequired(), validators.Length(min=2, max=30)])
email = EmailField('Email', [validators.DataRequired(), validators.Email()])
86995 次浏览

If you take a look at wtforms/validators.py file in line 9:

import email_validator

Just install the package:

pip install email_validator

I had the same problem with the latest updates, tried to install email_validator and flask-validator and continued with this exception. Solved by adding in requirements.txt the following line: email-validator == 1.0.5 as suggested [here].(https://github.com/alphagov/notifications-admin/commit/5ce2906c5aa6d16)

Actually wtforms[email]==2.3.1 is what I need.

If you want it installed with wtforms:

pip install wtforms[email]

Try install

pip install email-validator

This happened to me as well when i run it using virtual env. anaconda 3.7 However when i switched my project interpreter back to my local machine Python 3.7, then i run:

pip install email_validator

it worked fine.

I just found it strange that I couldn't install the module "email_validator" in my anaconda Project Interpreter. So i suggest that you try with local machine first.

Try install

pip install WTForms==2.1

you need pip install email-validator, wtforms depend on email-validator.

you can see email-validator module on Github https://github.com/JoshData/python-email-validator

From WTForms 2.3.0 version, the email validation is handled by an external library called email-validator (PR #429). If you want to enable email validation support, you either need to install WTForms with extra requires email:

$ pip install wtforms[email]

Or you can install email-validator directly:

$ pip install email-validator

Or you can back to the old version of WTForms:

$ pip install wtforms==2.2.1

P.S. If you are using Flask-WTF, except for install email-validator directly, you can also use email extra (if PR #423 got merged) in the next release (> 0.14.3).

In your project directory run:

pip install email_validator

This worked for me!

This should work as it worked for me. Just install this in project terminal:

pip install email-validator

I don't know if the root cause was that I used zsh in Terminal but I also get the error "zsh: no matches found: wtforms[email]" when I tried the command below.

pip install wtforms[email]

However, I tried to do the following command, it worked for me.

pip install -U "wtforms[email]"

from wtf forms

Validates an email address. Requires email_validator package to be installed. For ex: pip install wtforms[email].

pip install wtforms[email]


pip install email_validator

If we use wtforms.validators.Email in our python program, then it raise an exception that asks us to install the email_validator for email validation support. The solution for that problem is to type the following command in terminal pip install email_validator

Adding to what is already said for future referance,

pip install email_validator

P.S You dont need to import email_validator after installing.

If you have removed build dependencies before running your app, please check whether idna exists.

For Alpine Linux, use apk add py3-idna before installing build dependencies. Then this package will not be in the build dependency virtual package so will not be removed automatically.

Inside wtforms/validators.py file, line 392, you can see that there is exception handling for this occurrence in particular, which is why the following lines are executed to throw the alert

if email_validator is None:  # pragma: no cover
raise Exception("Install 'email_validator' for email validation
support.")

Solution is to pip install email-validator in the same location/directory for wtforms/validators.py sake.

I had the same error before:(