Home > Software engineering >  ModuleNotFoundError: No module named 'flask_mail'
ModuleNotFoundError: No module named 'flask_mail'

Time:11-09

I am creating a web application where the user will receive a confirmation mail after they've registered. But I am getting an error in the command line, Traceback (most recent call last): File "/usr/local/lib/python3.9/site-packages/flask/cli.py", line 240, in locate_app import(module_name) File "/home/ubuntu/flask/registration/application.py", line 3, in from flask_mail import Mail, Message ModuleNotFoundError: No module named 'flask_mail' and an error on the web page, Internal Server Error The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.

application.py

import os
from flask import Flask, redirect, render_template, request
from flask_mail import Mail, Message
from cs50 import SQL

app = Flask(__name__)
app.config["MAIL_DEFAULT_SENDER"] = os.getenv("MAIL_DEFAULT_SENDER")
app.config["MAIL_PASSWORD"] = os.getenv("MAIL_PASSWORD")
app.config["MAIL_PORT"] = 587
app.config["MAIL_SERVER"] = smtp.gmail.com
app.config["MAIL_USE_TLS"] = True
app.config["MAIL_USERNAME"] = os.getenv("MAIL_USERNAME")
mail = Mail(app)


db = SQL("sqlite:///froshims.db")
REGISTRANTS = {}

SPORT=["Cricket", "Football", "Badminton", "Kho-Kho", "Kabaddi"]

@app.route("/", methods = ["GET", "POST"])
def index():
    if request.method == "GET":
        return render_template("index.html", sports = SPORT)
    email = request.form.get("email")
    sport = request.form.get("sport")
    if not email:
        return render_template("failure.html", message="E-mail not entered")
    if not sport:
        return render_template("failure.html", message="Sport not selected")
    if sport not in SPORT:
        return render_template("failure.html", message="Sport not in list. Don't try to hack our website.")
    if request.method == "POST":
        REGISTRANTS[name]=sport
        print("yes")
        db.execute("INSERT INTO registrants (name, sport) VALUES (?,?)", name, sport)
        message = Message("You are registered!", recipients=[email])
        mail.send(message)
        return redirect("success")

@app.route("/success")
def success():
    print("kaam kar na")
    registrants = db.execute("SELECT * FROM registrants")
    return render_template("success.html", registrants = registrants)

I am unable to figure out the issue. I am new to Flask. Please guide me.

CodePudding user response:

I had the same problem with flask_mail import. pip installing Flask-Mail in a virtual environment on a code editor alone might not cause a module import error. Go to your command line into the app directory Also, make sure the pip version in your venv environment is up to date.

pip install Flask-Mail

This worked for me.

  • Related