3

I´m new to the flask and started to create this small application, I want to send emails, but when flask imports the configuration of the init file, I get the error "ImportError: can not import name mail". I already read that it has to do with the structure of my application but I can not accommodate it so there are no problems. thanks for your help

This is the structure of my application:

└── my-project
    ├── app
    │   ├── __init__.py
    │   ├── constants
    │   │   ├── __init__.py
    │   │   ├── constants.py
    │   │   
    │   ├── models
    │   │    └── __init__.py
    │   │    ├── conectionDB.py
    │   │    ├── Erros.py
    │   │    ├── Session.py
    │   │    ├── Users.py
    │   │
    │   ├── resources
    │   │   ├── __init__.py
    │   │   └── Mail.py
    │   │   └── Session.py 
    │   ├── utils
    │   │   ├── __init__.py
    │   │   └── api_v1.py      
    │   
    ├── database
    │      ├── init.sql
    │
    ├── config
    │   ├── __init__.py
    │   ├── default.py
    │   
    └── api.py

api.py

import os
from app import app

if __name__ == '__main__':
    app.run(host=os.environ["API_IP"], port=5000, debug=os.environ["API_DEBUG"])

app/__ init __.py

from flask import Flask
from flask_mail import Mail
from app.apiL import blueprint as apiL

# logging.config.fileConfig('config/development-logging.conf')

app = Flask(__name__)
app.config.from_object('config.default')


# app.config.from_pyfile('config.py')
app.register_blueprint(api_v1)
mail = Mail(app)

resources/Mail.py

from app import mail # you can now import the Mail() object
from flask_mail import Message


class EmailSend:

    def send_email(subject, sender, recipients, text_body):
        msg = Message(subject=subject, sender=sender, recipients=recipients)
        msg.body = text_body
        #msg.html = html_body
        mail.send(msg)

The following message appears when executing:

| Traceback (most recent call last):
api_1  |   File "api.py", line 2, in <module>
api_1  |     from app import app
api_1  |   File "/usr/src/api/app/__init__.py", line 5, in <module>
api_1  |     from app.api_v1 import blueprint as api_v1
api_1  |   File "/usr/src/api/app/api_v1.py", line 4, in <module>
api_1  |     from app.resources.Session import nsLogIn, nsSignUP, nsVerifyEmail
api_1  |   File "/usr/src/api/app/resources/Session.py", line 19, in <module>
api_1  |     from app.resources.Mail import EmailSend
api_1  |   File "/usr/src/api/app/resources/Mail.py", line 1, in <module>
api_1  |     from app import mail  # you can now import the Mail() object
api_1  | ImportError: cannot import name 'mail'

thanks for your help.

1 Answers1

0

It's possible that you have a circular dependency problem. In resources/Mail.py file, try to import mail module inside send_email function.

Your resources/Mail.py file will be similar to this:

from flask_mail import Message


class EmailSend:

    def send_email(subject, sender, recipients, text_body):
        from app import mail # you can now import the Mail() object
        msg = Message(subject=subject, sender=sender, recipients=recipients)
        msg.body = text_body
        #msg.html = html_body
        mail.send(msg)

Please see ImportError: cannot import name mail in Flask and ImportError: cannot import name mail.

ASSILI Taher
  • 1,210
  • 2
  • 9
  • 11