PythonEnthusiast PythonEnthusiast - 3 months ago 165
Python Question

Celery - ImportError: No module named tasks

I want to use Celery in Flask. Here's my directory listing:-

c26_calendar
|-- c26_calendar
| |-- celery_config.py
| |-- __init__.py
| |-- settings.py
|-- manage.py
|-- modules
| |-- __init__.py
| |-- __init__.pyc
| |-- messages
| | |-- __init__.py
| | |-- models.py
| | |-- views.py
| | |-- tasks.py
| |-- tasks
| | |-- __init__.py
| | |-- models.py
| | |-- views.py
| `-- user
| |-- fb_keys.json
| |-- fb.py
| |-- helpers.py
| |-- __init__.py
| |-- models.py
| |-- tasks.py
| |-- views.py
|-- __init__.py


My
c26_calendar/c26_calendar/__init__.py
is

from celery import Celery
from flask import Flask
from flask.ext.mongoengine import MongoEngine

app = Flask(__name__, static_url_path='')
app.config["MONGODB_SETTINGS"] = {'DB': "calendar"}
app.config["SECRET_KEY"] = ""

celery = Celery('c26_calendar')
celery.config_from_object('c26_calendar.celery_config')

db = MongoEngine(app)


def register_blueprints(app):
from modules.user.views import user
from modules.messages.views import messages
from modules.tasks.views import tasks

app.register_blueprint(user)
app.register_blueprint(messages)
app.register_blueprint(tasks)

register_blueprints(app)


if __name__ == '__main__':
app.run()


My
c26_calendar/celery_config.py
is

CELERY_BROKER_URL = 'redis://localhost:6379/0'
CELERY_RESULT_BACKEND = 'redis://localhost:6379/0'
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
CELERY_ACCEPT_CONTENT = ['json']
CELERY_TIMEZONE = 'Europe/Oslo'
CELERY_ENABLE_UTC = True
CELERY_IMPORTS = ('tasks')
CELERY_CREATE_MISSING_QUEUES = True
CELERY_DEFAULT_RATE_LIMIT = '10/m'
CELERYD_MAX_TASKS_PER_CHILD = 10


PS: There is a module called
tasks
which has nothing to do with celery tasks.

I want to include all the tasks from every module. I ran the following command from inside the root application directory, ie

celery -A c26_calendar.celery worker


It throws an error saying
ImportError: No module named tasks
. Following is the stack-trace:-

Traceback (most recent call last):
File "/usr/local/bin/celery", line 11, in <module>
sys.exit(main())
File "/usr/local/lib/python2.7/dist-packages/celery/__main__.py", line 30, in main
main()
File "/usr/local/lib/python2.7/dist-packages/celery/bin/celery.py", line 81, in main
cmd.execute_from_commandline(argv)
File "/usr/local/lib/python2.7/dist-packages/celery/bin/celery.py", line 769, in execute_from_commandline
super(CeleryCommand, self).execute_from_commandline(argv)))
File "/usr/local/lib/python2.7/dist-packages/celery/bin/base.py", line 311, in execute_from_commandline
return self.handle_argv(self.prog_name, argv[1:])
File "/usr/local/lib/python2.7/dist-packages/celery/bin/celery.py", line 761, in handle_argv
return self.execute(command, argv)
File "/usr/local/lib/python2.7/dist-packages/celery/bin/celery.py", line 693, in execute
).run_from_argv(self.prog_name, argv[1:], command=argv[0])
File "/usr/local/lib/python2.7/dist-packages/celery/bin/worker.py", line 179, in run_from_argv
return self(*args, **options)
File "/usr/local/lib/python2.7/dist-packages/celery/bin/base.py", line 274, in __call__
ret = self.run(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/celery/bin/worker.py", line 212, in run
state_db=self.node_format(state_db, hostname), **kwargs
File "/usr/local/lib/python2.7/dist-packages/celery/worker/__init__.py", line 95, in __init__
self.app.loader.init_worker()
File "/usr/local/lib/python2.7/dist-packages/celery/loaders/base.py", line 128, in init_worker
self.import_default_modules()
File "/usr/local/lib/python2.7/dist-packages/celery/loaders/base.py", line 121, in import_default_modules
tuple(maybe_list(self.app.conf.CELERY_INCLUDE))
File "/usr/local/lib/python2.7/dist-packages/celery/loaders/base.py", line 103, in import_task_module
return self.import_from_cwd(module)
File "/usr/local/lib/python2.7/dist-packages/celery/loaders/base.py", line 112, in import_from_cwd
package=package,
File "/usr/local/lib/python2.7/dist-packages/celery/utils/imports.py", line 101, in import_from_cwd
return imp(module, package=package)
File "/usr/local/lib/python2.7/dist-packages/celery/loaders/base.py", line 106, in import_module
return importlib.import_module(module, package=package)
File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)


What wrong am I doing? How do I run the celery?

Answer

Your base path is /path/to/c26_calendar/. Every paths must be relative to base path.

Instead of

CELERY_IMPORTS = ('tasks')

use

CELERY_IMPORTS = ('modules/tasks',)
Comments