Forums

ImportError:cannot import name 'current_app'

I have an Flask app that allows users to register using an online web form. In order to do that, after the form has been filled up the user will receive a confirmation email with a link to confirm, which will allow the app to set a authentication flags in a Mysql database. The app runs without any problem on localhost. On pythonanywhere, is it possible to fill up the form on line, but the confirmation email is never sent. The email.py is:

from threading import Thread
from flask import current_app, render_template
from flask.ext.mail import Message
from . import mail


def send_async_email(app, msg):
    with app.app_context():
        mail.send(msg)


def send_email(to, subject, template, **kwargs):
    app = current_app._get_current_object()
    msg = Message(app.config['FLASKY_MAIL_SUBJECT_PREFIX'] + ' ' + subject,
                  sender=app.config['FLASKY_MAIL_SENDER'], recipients=[to])
    msg.body = render_template(template + '.txt', **kwargs)
    msg.html = render_template(template + '.html', **kwargs)
    thr = Thread(target=send_async_email, args=[app, msg])
    thr.start()
    return thr

When I try to run this file. The error that comes up is: ImportError:cannot import name 'current_app'. I looks that there is something wrong with flask ? Any idea ?

Help docs here

Thank you for the information. If I follow the steps inDebugging sys.path issues in web apps. When I type from flask import current_app, render_template in the virtualenv console, it works. Not import error is reported. I went to the Virtualenv section, and enter the path: /home/myusername/.virtualenvs/myvirtualenv However, It seems that the web app is not "recognizing" properly the virtualenv. I am not importing a module from my app file structure. I am importing from flask, which with a proper virtualenv should not be a problem. None of my app modules is named flask. Any suggestions ?

Ah. I missed the bit where you said "When I try to run this file" and I missed that you're starting threads. How are you trying to run the file? Are you sure the virtualenv is activated when you run the file? As to the threads bit, you can't start a thread in a PythonAnywhere web app.

When I noticed that confirmation emails are not sent, I went to Dashboard then File then Run this file, and in the console came up this error .. Now that say that, I think, the file will run in a console with Python3.x but not in a virtualenv.. When I start the virtualenv console it works..Thank you for pointing out that... I will check others reasons why confirmation emails are not sent. However, now that you say that you can't start a thread in a PythonAnywhere web app. What other option can I use to implement a module like the one I am using to send the confirmation emails ?

Just don't use a thread. Like this:

def send_email(to, subject, template, **kwargs):
    app = current_app._get_current_object()
    msg = Message(app.config['FLASKY_MAIL_SUBJECT_PREFIX'] + ' ' + subject,
                  sender=app.config['FLASKY_MAIL_SENDER'], recipients=[to])
    msg.body = render_template(template + '.txt', **kwargs)
    msg.html = render_template(template + '.html', **kwargs)
    mail.send(msg)
return thr