1

I'm having problems integrating Celery in my Flask application. This is the repo https://github.com/theobouwman/community-python.

I start my app by running app.py which imports my app (where blueprints and config are added) and Celery.

In /tasks/add.py I have a sample task and where I import the Celery object again for the @celery.task decorator.

Till that point everything works fine. I can run my Flask application and run the Celery worker.

But when I try to trigger a task from within a controller in a Blueprint like here https://github.com/theobouwman/community-python/blob/master/auth/controllers/RegistrationController.py#L38 it says that it cannot import it, which it a logic reaction.

Traceback (most recent call last):
  File "app.py", line 2, in <module>
    from flask_app import app
  File "/development/projects/python/Community/flask_app.py", line 4, in <module>
    from auth.routes import auth
  File "/development/projects/python/Community/auth/routes.py", line 3, in <module>
    from .controllers import RegistrationController, AuthenticationController, LogoutController
  File "/development/projects/python/Community/auth/controllers/RegistrationController.py", line 10, in <module>
    from tasks.add import add
  File "/development/projects/python/Community/tasks/add.py", line 1, in <module>
    from app import celery
  File "/development/projects/python/Community/app.py", line 2, in <module>
    from flask_app import app
ImportError: cannot import name 'app'

I don't know how to fix this import cycle and that's the reason for this question. I googled for like 3 hours but couldn't find a solution. I hope someone here could help me.

And is there a Flask Slack or Gitter in the air?

Thanks in advance.

2ps
  • 15,099
  • 2
  • 27
  • 47
Dirk
  • 3,095
  • 4
  • 19
  • 37

1 Answers1

2

Change your import in RegistrationController.py to a local one to solve the circular import:


    from ..blueprint import auth
    from models import User
    from flask import redirect, url_for, request, render_template, flash
    import bcrypt
    from ..forms.register import SimpleRegistrationForm
    """
    Error in python3.6 app.py
    Says cyclus import error
    """
    # Comment out the line below
    # from tasks.add import add


    @auth.route('/register', methods=['GET', 'POST'])
    def register():
        form = SimpleRegistrationForm(request.form)
        if request.method == 'POST' and form.validate():
            fname = request.form['fname']
            sname = request.form['sname']
            email = request.form['email']
            password = request.form['password']
            hashed = bcrypt.hashpw(password.encode('utf-8 '), bcrypt.gensalt())

            user = User.select().where(User.email == email)
            if user.exists():
                flash('Er bestaat al een account met dit email adres')
                return redirect(url_for('auth.register'))

            user = User(fname=fname, sname=sname, email=email, password=hashed)
            user.save()

            flash('Uw account is aangemaakt. Kijk in uw mailbox voor de activatie link')
            return redirect(url_for('auth.register'))
        return render_template('pages/register.html', form=form)


    @auth.route('/register/test')
    def register_test():
        # local import avoids the cycle
        from tasks.add import add
        add.delay()
        # hashed = bcrypt.hashpw('test'.encode('utf-8 '), bcrypt.gensalt())
        # user = User(
        #     fname='Theo',
        #     sname='Bouwman',
        #     email='theobouwman98@gmail.com',
        #     password=hashed
        # )
        # user.save()
    return redirect(url_for('auth.login'))
2ps
  • 15,099
  • 2
  • 27
  • 47