103

Getting exception when running the following code for form validation.

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.

Runs perfectly on codeanywhere VM. Does not on local machine.

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()])
azmeuk
  • 4,026
  • 3
  • 37
  • 64
VeeDuvv
  • 1,131
  • 2
  • 5
  • 6
  • The stack trace could use some formatting. – Sid Apr 22 '20 at 04:08
  • 5
    error itself says `Exception: Install 'email_validator' for email validation support.` install `email_validator`.just install `email_validator` using `pip install email-validator` command. i had same problem just solved after installing `email-validator` – M_x Jul 17 '20 at 13:28
  • It is kind of dumb that we need another pip dependency for just email validation. – new name Mar 11 '23 at 14:55

17 Answers17

111

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

import email_validator

Just install the package:

pip install email_validator
ivan_filho
  • 1,371
  • 1
  • 10
  • 13
  • 3
    Even the exception makes the solution clear: `Exception: Install 'email_validator' for email validation support.` – dspencer Apr 22 '20 at 03:49
  • 3
    My bad ppl.. I was curious why it explicitly asked to do so on my local instance when it didn't on my vm. was trying to figure out how to anticipate and avoid such cross machine/platform errors from the beginning. – VeeDuvv Apr 22 '20 at 12:22
  • 5
    I encountered the same issue just today, where the app runs fine on my local but throws this error on the VM. WTForms released a new version 2.3.1 just today which broke things. Going back to 2.2.1 fixed it for me. – absk Apr 22 '20 at 17:31
  • @VeeDuvv Consider that install has a broad meaning so out of context it can mean a lot more than just to run pip install. – Toaster Dec 20 '20 at 19:15
  • I had the same issue with WTForms 2.3.3. 'pip install email-validator' solved it. email-validator version: 1.1.3. Although it installed a few other packages: idna and dnspython. – sipi09 Sep 01 '21 at 08:38
  • To avoid issues in VMs, make sure to include email_validator in your requirements.txt file. – Polymeron Jan 11 '22 at 13:03
26

If you want it installed with wtforms:

pip install wtforms[email]
munsu
  • 1,914
  • 19
  • 24
  • 1
    I got the message: no matches found: wtforms[email] – Michael Dec 21 '20 at 14:01
  • @MichaelLossagk I tried this just now (Python 3.7.9) and it still works. Moreover, the option for email is still in their config: https://github.com/wtforms/wtforms/blob/master/setup.cfg – munsu Jan 08 '21 at 02:26
22

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).

Grey Li
  • 11,664
  • 4
  • 54
  • 64
8

Try install

pip install email-validator
Ardent Coder
  • 3,777
  • 9
  • 27
  • 53
Jorge Gomez
  • 119
  • 3
5

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.

bertha
  • 113
  • 3
4

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.

Celio Marcos
  • 129
  • 1
  • 6
3

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

sunyunxian
  • 71
  • 4
3

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

pip install email-validator

Er. Sachish
  • 369
  • 2
  • 13
3

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:(

2

Try install

pip install WTForms==2.1 
周小明
  • 37
  • 1
  • 3
    Hello! While this code may solve the question, [including an explanation](https://meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit] your answer to add explanations and give an indication of what limitations and assumptions apply. – Brian61354270 Jun 25 '20 at 18:27
  • This also did not solve the issue, as I've that version of WTForms, and I get the same exception. – toonarmycaptain Sep 28 '20 at 02:50
2

Adding to what is already said for future referance,

pip install email_validator

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

Thesonter
  • 182
  • 12
1

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
1

Installing the dependency email_validator as following solved the issue:

pip3 install email_validator
allexiusw
  • 1,533
  • 2
  • 16
  • 23
erdoganb
  • 41
  • 4
0

In your project directory run:

pip install email_validator

This worked for me!

mrm
  • 59
  • 9
0

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]"
Gobo
  • 33
  • 6
0

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

Sherin
  • 9
  • 1
0

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.

Steven Yang
  • 153
  • 5