Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

importing celery in project fails, works(sort of) in manage.py shell

Im going over this list here but i cant get it working :

http://docs.celeryproject.org/en/latest/django/first-steps-with-django.html

So what i did was: 1) downloaded celery modules and added them to my manage.py and wsgi.py:

# -*- coding: utf-8 -*-
#!/usr/bin/env python
import os, sys
from ConfigParser import RawConfigParser

config = RawConfigParser()
abspath = os.path.abspath(os.path.dirname(__file__))

config.read(abspath+'/subfolder/settings.ini')

homedir = config.get('paths', 'libspath')
projectspath = config.get('paths', 'projectspath')

path = [
    '/Django-1.4.5/', 
    '/South-0.7.6',
    '/python-openid-master',#07.05.2013 checkout from https://github.com/openid/python-openid
    '/mbi-django-rosetta-eca151e',
    '/phonenumbers-5.2b1',
    '/django-phonenumber-field-develop',
    '/django-openid-auth-0.5',
    '/celery-3.0.19',
    '/django-celery-3.0.17',
    '/kombu-2.5.11',
    '/billiard-2.7.3.28',
    '/anyjson-0.3.3',
    '/amqp-1.0.12'
    ]

for item in path:
    module = homedir+item
    if module not in sys.path:
        sys.path.append(module)

2) installed rabbitmq 3) set up stuff in settings.py:

INSTALLED_APPS = (
    ---
    'djcelery',
)

and in the end:

BROKER_URL = 'amqp://guest:guest@localhost:5672/'
import djcelery
djcelery.setup_loader()

4) ran python manage.py syncdb

5) since im using mod_wsgi i also added this to my wsgi.py (in addition to code i showed earlier)

import djcelery
djcelery.setup_loader()

6) i created tasks.py in my core app:

import logging, subprocess
logger = logging.getLogger('debugger')

from django.conf import settings

from celery import task

@task
def runfunc(funcname, refno):
    x = 'nothing to see here'

7) i run: python manage.py celery worker --loglevel=info and get whole bunch of stuff meaning that its all good :P.

But when i go the django view which is supposed to run the task i get:

cannot import name task
Request Method: GET
Request URL:    http://localhost/url/that/triggers/task/
Django Version: 1.4.5
Exception Type: ImportError
Exception Value:    
cannot import name task
Exception Location: /path/to/project/core/tasks.py in <module>, line 6

When i go to manage.py shell and type from celery import task - it works just fine. if i import the function from core.tasks, then i get exactly same error message.

Can anyone explain to me wth is up here..

Alan

like image 988
Odif Yltsaeb Avatar asked Jan 28 '26 14:01

Odif Yltsaeb


2 Answers

Try altering tasks.py to:

from __future__ import absolute_import
from celery import task

It's possible that you have a celery/ or celery.py in the same directory as the module throwing the error (tasks.py), and this is failing as a relative import.

like image 178
mik3y Avatar answered Jan 31 '26 02:01

mik3y


Yikes, where did you get all that in your first step?? I've never had to alter the manage.py or wsgi.py files to get additional packages running. You say "downloaded celery modules" and then you have all that sys.path alteration code. That leads me to believe you aren't using pip and virtualenv. Your life will be massively easier using those two tools. You'll simplify things so much your team will love you forever. Don't over-complicate when Python package management exists to make this pain free. Path mangling is rarely necessary.

Those Celery first steps you reference are really all it takes. Looks like you've handled that special mod_wsgi note. My guess is the problem lies in the first step -- whatever modifications you've made to manage.py and wsgi.py. All the other steps look fine*.

I'd make a copy of your code, make a new virtualenv, pip install those packages and remove all the path setup stuff. Use django-admin.py startproject xxx to see what plain vanilla manage.py and wsgi.py files look like. Also use DEBUG=True to help with the troubleshooting. When that works and you get things settled, replace your settings.ini with a pip requirements file.

* You use South so remember python manage.py migrate djcelery. (In your step 4 you only use syncdb). I don't believe doing this will fix the import error but it's necessary in the larger picture.

like image 32
JCotton Avatar answered Jan 31 '26 01:01

JCotton



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!