使用 pip 安装 email_validator

Install email_validator using pip

我已经安装了 email_validator,但是我收到一条错误消息说它没有安装。 它必须安装在我的项目文件夹中吗?或者它可以在主站点包文件夹中吗?当我 运行 我的程序时,我收到此错误消息:

Exception: Install 'email_validator' for email validation support

我可以看到我已将它安装在主站点包 python3 文件夹中,但是当我 运行

pip install email_validator

在我的项目文件夹 site-packages 文件夹中,终端打印如下:

site-packages % pip install email_validator
Defaulting to user installation because normal site-packages is not writeable
Requirement already satisfied: email_validator in /Users/STL34/Library/Python/3.7/lib/python/site-packages (1.1.1)
Requirement already satisfied: idna>=2.0.0 in /Users/STL34/Library/Python/3.7/lib/python/site-packages (from email_validator) (2.10)
Requirement already satisfied: dnspython>=1.15.0 in /Users/STL34/Library/Python/3.7/lib/python/site-packages (from email_validator) (1.16.0)

并且没有安装在我的项目文件夹中。

代码如下:

from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, SubmitField, BooleanField
from wtforms.validators import DataRequired, Length, Email, EqualTo


class RegistrationForm(FlaskForm):
    username = StringField('Username',
                           validators=[DataRequired(), Length(min=2, max=20)])
    email = StringField('Email',
                        validators=[DataRequired(), Email()])
    password = PasswordField('Password', validators=[DataRequired()])
    confirm_password = PasswordField('Confirm Password',
                                     validators=[DataRequired(), EqualTo('password')])
    submit = SubmitField('Sign Up')


class LoginForm(FlaskForm):
    email = StringField('Email',
                        validators=[DataRequired(), Email()])
    password = PasswordField('Password', validators=[DataRequired()])
    remember = BooleanField('Remember Me')
    submit = SubmitField('Login')

我的项目中没有出现是什么意思。你能post做

的输出吗
from email_validator import validate_email, EmailNotValidError

在我们的 python 文件中
如果您正在使用 pyenv,那么也从 pyenv 安装该模块。

最简单的包管理方式是 Virtual environments

email_validator 示例

sudo apt-get install -y python3-venv python3-pip
mkdir email
cd email
deactivate 2> /dev/null
pip3 show virtualenv
if [ $? -ne 0 ] ; then
   pip3 install --upgrade pip
   pip3 install --upgrade setuptools
   pip3 install virtualenv
fi

# now lets build venv
python3 -m venv venv
source venv/bin/activate
pip3 install email_validator
echo "import email_validator" > email.py
echo "print(email_validator.validate_email('hello@gmail.com'))" >> email.py
python3 email.py

输出

Reading package lists... Done
Building dependency tree       
Reading state information... Done
python3-pip is already the newest version (20.0.2-5ubuntu1).
python3-venv is already the newest version (3.8.2-0ubuntu2).
0 upgraded, 0 newly installed, 0 to remove and 41 not upgraded.
Name: virtualenv
Version: 20.0.25
Summary: Virtual Python Environment builder
Home-page: https://virtualenv.pypa.io/
Author: Bernat Gabor
Author-email: gaborjbernat@gmail.com
License: MIT
Location: /home/mcs/.local/lib/python3.8/site-packages
Requires: distlib, appdirs, six, filelock
Required-by: 
Collecting email_validator
  Using cached email_validator-1.1.1-py2.py3-none-any.whl (17 kB)
Collecting idna>=2.0.0
  Using cached idna-2.10-py2.py3-none-any.whl (58 kB)
Collecting dnspython>=1.15.0
  Using cached dnspython-1.16.0-py2.py3-none-any.whl (188 kB)
Installing collected packages: idna, dnspython, email-validator
Successfully installed dnspython-1.16.0 email-validator-1.1.1 idna-2.10
<ValidatedEmail hello@gmail.com>

我解决了这个问题。我使用带有 -t 标志的 pip 在我的项目文件夹站点包文件夹中安装 email_validator。如果没有 -t 标志,pip 会在 Python3 站点包文件夹中安装 email_validator,这不起作用。