Scheduled task and Flask-mail


I'm trying to run this simple code:

from flask_app import app
from flask_mail import Mail, Message

mail = Mail(app)

msg = Message("Testing daily mail", recipients=[""])
msg.body = "Beep boop I'm a bot sending an email"
msg.html = "<b>testing</b>"

And I get this on my log:

Traceback (most recent call last):
  File "/home/billpest/mysite/", line 3, in <module>
    from flask_app import app
  File "/home/billpest/mysite/", line 38, in <module>
    from flask_user import roles_required, UserManager, UserMixin, current_user, login_required
ImportError: No module named flask_user

2020-11-04 20:25:15 -- Completed task, took 7.60 seconds, return code was 1.

I'm pretty sure I have flask-user installed since I'm running it without any problems on my main app; this error appears when I try a scheduled task.

Maybe I am doing something wrong but I don't see it.

Any help would be appreciated.


You must be running your task in a different environment than the one you installed flask-user for.

You need to specify which python interpreter to use. It can be done as part of the command or in the #! line on top of the script.

Thank you for your answer.

I'm not using any virtual environment so I typed

#!/home/billpest/.local/bin python3

And I still can't run it. Is that line ok?

EDIT: Now I used


And got the same error :(

EDIT2: Looks like this worked


But now I get this new error:

Traceback (most recent call last):
  File "/home/billpest/mysite/", line 9, in <module>
    msg = Message("Testing daily mail", recipients=[""])
  File "/home/billpest/.local/lib/python3.8/site-packages/", line 273, in __init__
    sender = sender or current_app.extensions['mail'].default_sender
  File "/usr/lib/python3.8/site-packages/werkzeug/", line 348, in __getattr__
    return getattr(self._get_current_object(), name)
  File "/usr/lib/python3.8/site-packages/werkzeug/", line 307, in _get_current_object
    return self.__local()
  File "/usr/lib/python3.8/site-packages/flask/", line 52, in _find_app
    raise RuntimeError(_app_ctx_err_msg)
RuntimeError: Working outside of application context.

This typically means that you attempted to use functionality that needed
to interface with the current application object in some way. To solve
this, set up an application context with app.app_context().  See the
documentation for more information.

2020-11-05 15:32:24 -- Completed task, took 16.20 seconds, return code was 1.

Can you guide me to the solution please? :D

Thank you!

The error message rightly points you at the Flask documentation in order to understand what is going on there and how to fix it -- this page is the right place to start.

Giles, thank you very much, that solved my issue.

In case anyone in the future stumble upon this post, my final code is:

# coding=utf-8

from flask_app import app, mail, Message

with app.app_context():

    msg = Message("Testing daily mail", sender = 'MailBot', recipients=[""])
    msg.body = "Beep boop I'm a bot sending an email"
    msg.html = "<b>testing</b>"

Thanks for the information!

Error running WSGI application 2021-07-06 17:20:44,617: ModuleNotFoundError: No module named 'flask_mail'
2021-07-06 17:20:44,617: File "/var/www/", line 16, in <module>

I am getting this error help me plz, really greatful if anyone could

It sounds like you need to install the flask-mail package for the environment where your website is running. It looks like it's running Python 3.10 with no virtualenv, so the Bash command

pip3.10 install --user flask-mail

...should do the trick. See this help page for more information.