简体   繁体   中英

How to solve this celery Error: Unable to load celery application. Module 'forecast' has no attribute 'celery'

I am trying to seting up Celery and Amazon SQS for my project but faced with problems. I did following. I put celery.py file inside my project directory where settings py located. Here is snapshot of my project

--predictions
     --forecast
        --dataflow
        --forecast
              **  __init__.py
              **  celery.py
              **  urls.py
               ** settings.py
               ** wsgi.py
        --manage.py
     --env

I doted with "--" folders and with "**" files. Inside celery.py file i have the folliwing code

from __future__ import      
absolute_import, unicode_literals
import os
from celery import Celery                       


os.environ.setdefault
('DJANGO_SETTINGS_MODULE',
'forecast.settings')

app = Celery('forecast')
app.config_from_object
('django.conf:settings')

app.autodiscover_tasks()

app.conf.beat_schedule = {
'display_time-30-seconds':   
{ 'task':  'demoapp.tasks.display_time',
    'schedule': 10.0
 },
}                                                 
@app.task(bind=True)                             
def debug_task(self):
    print('Request:     
    {0r}'.format(self.request))

Inside settings.py file i set up Amazon credentials,broker_url etc

#settings.py
# AWS Credentials                                 
AWS_ACCESS_KEY_ID = ('lol')      
AWS_SECRET_ACCESS_KEY =    
('Lol')                                                                              

# Celery                                          
BROKER_URL = "sqs://%s:%s@" %   
(AWS_ACCESS_KEY_ID, 
AWS_SECRET_ACCESS_KEY)                        
CELERY_ACCEPT_CONTENT = 
['application/json']    
CELERY_RESULT_SERIALIZER = 'json'               
CELERY_TASK_SERIALIZER = 'json'                 
CELERY_DEFAULT_QUEUE = 'test.fifo'              
CELERY_RESULT_BACKEND = None # 
Disabling the results backend

BROKER_TRANSPORT_OPTIONS =    
{'region': 'us-west-2',                          
'polling_interval': 20,}

Inside init .py file i put the following code

from __future__ import   
absolute_import, unicode_literals
from .celery import app as 
celery_app__all__ = ['celery_app']

Then i added in my django app task.py which contain the following code

from datetime import datetime                   
from celery import shared_task                  
@shared_task
def display_time(x, y):                                
     print("The time is %s :" % 
     str(datetime.now()))                                                 
     return True

after it i create inside AWS default queue. Than inside predictions folder in terminal i run the following command

celery -A forecast worker -l info

Which give me Error

Error: Unable to load celery 
application. Module 'forecast' has no 
attribute 'celery'

When i run this command inside predictions/forecast folder its give traceback about many errors some of them in models.py file. Here is Traceback

Traceback (most recent call last):
File "/data/data/com.termux/files/
home/storage/predictions/env/bin/
celery", line 10, in <module>
sys.exit(main())
File "/data/data/com.termux/files/
home/storage/predictions/env/lib/   
python3.7/site-packages/celery/ 
__main__.py", line 16, in main
_main()
File "/data/data/com.termux/files/   
home/storage/predictions/env/lib/
python3.7/site-packages/celery/bin/
celery.py", line 322, in main
cmd.execute_from_commandline
(argv)
File "/data/data/com.termux/files/
home/storage/predictions/env/lib/
python3.7/site-packages/celery/bin/
celery.py", line 496, in 
execute_from_commandline
super(CeleryCommand, self)
.execute_from_commandline(argv)))
File "/data/data/com.termux/files/
home/storage/predictions/env/lib/
python3.7/site-packages/celery/bin/ 
base.py", line 298, in 
execute_from_commandline
return 
self.handle_argv(self.prog_name, 
argv[1:])
File "/data/data/com.termux/files/  
home/storage/predictions/env/lib/
python3.7/site-packages/celery/bin/
celery.py", line 488, in handle_argv
return self.execute(command, argv)
File "/data/data/com.termux/files/  
home/storage/predictions/env/lib/
python3.7/site-packages/celery/bin/
celery.py", line 420, in execute
).run_from_argv(self.prog_name, 
argv[1:], command=argv[0])
File "/data/data/com.termux/files/
home/storage/predictions/env/lib/
python3.7/site-packages/celery/bin/
worker.py", line 223, in run_from_argv
return self(*args, **options)
File "/data/data/com.termux/files/
home/storage/predictions/env/lib/
python3.7/site-packages/celery/bin/
base.py", line 252, in __call__
ret = self.run(*args, **kwargs)
return 
self.handle_argv(self.prog_name,   
argv[1:])
File "/data/data/com.termux/files/   
home/storage/predictions/env/lib/
python3.7/site-packages/celery/bin/
celery.py", line 488, in handle_argv
return self.execute(command, argv)
File "/data/data/com.termux/files/
home/storage/predictions/env/lib/
python3.7/site-packages/celery/bin/
celery.py", line 420, in execute
).run_from_argv(self.prog_name,    
argv[1:], command=argv[0])
File "/data/data/com.termux/files/ 
home/storage/predictions/env/lib/
python3.7/site-packages/celery/bin/
worker.py", line 223, in run_from_argv
return self(*args, **options)
File "/data/data/com.termux/files/
home/storage/predictions/env/lib/
python3.7/site-packages/celery/bin/
base.py", line 252, in __call__
ret = self.run(*args, **kwargs)
File "/data/data/com.termux/files/
home/storage/predictions/env/lib/
python3.7/site-packages/celery/bin/
worker.py", line 257, in run
**kwargs)
File "/data/data/com.termux/files/   
home/storage/predictions/env/lib/
python3.7/site-packages/celery/bin/
worker.py", line 223, in run_from_argv
return self(*args, **options)
File "/data/data/com.termux/files/
home/storage/predictions/env/lib/
python3.7/site-packages/celery/bin/
base.py", line 252, in __call__
ret = self.run(*args, **kwargs)
File "/data/data/com.termux/files/
home/storage/predictions/env/lib/
python3.7/site-packages/celery/bin/
worker.py", line 257, in run
**kwargs)
File "/data/data/com.termux/files/
home/storage/predictions/env/lib/
python3.7/site-packages/celery/
worker/worker.py", line 96, in __init__
self.app.loader.init_worker()
File "/data/data/com.termux/files/  
home/storage/predictions/env/lib/
python3.7/site-packages/celery/
loaders/base.py", line 114, in  
init_worker
self.import_default_modules()
File "/data/data/com.termux/files/  
home/storage/predictions/env/lib/
python3.7/site-packages/celery/
loaders/base.py", line 108, in 
import_default_modules
File "/data/data/com.termux/files/     
home/storage/predictions/env/lib/
python3.7/site-packages/celery/utils/
dispatch/signal.py", line 288, in send
response = receiver(signal=self,    
sender=sender, **named)
File "/data/data/com.termux/files/
home/storage/predictions/env/lib/
python3.7/site-packages/celery/
fixups/django.py", line 84, in 
on_import_modules
self.worker_fixup.validate_models()
File "/data/data/com.termux/files/
home/storage/predictions/env/lib/
python3.7/site-packages/celery/
fixups/django.py", line 122, in 
validate_models
self.django_setup()
File "/data/data/com.termux/files/
home/storage/predictions/env/lib/
python3.7/site-packages/celery/
fixups/django.py", line 118, in    
django_setup
django.setup()
File "/data/data/com.termux/files/
home/storage/predictions/env/lib/
python3.7/site-packages/django/ 
__init__.py", line 24, in setup  
apps.populate
(settings.INSTALLED_APPS)
File "/data/data/com.termux/files/
home/storage/predictions/env/lib/
python3.7/site-packages/django/   
apps/registry.py", line 114, in populate
app_config.import_models()

File "/data/data/com.termux/files/
home/storage/predictions/env/lib/
python3.7/site-packages/django/
apps/config.py", line 211, in 
import_models
self.models_module=import_module
(models_module_name)

File "/data/data/com.termux/files/
home/storage/predictions/env/lib/
python3.7/importlib/__init__.py", line 
127, in import_module  return 
_bootstrap._gcd_import(name[level:], 
package, level)
File "<frozen importlib._bootstrap>", 
line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", 
line 983, in _find_and_load

File "<frozen importlib._bootstrap>", 
line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", 
line 677, in _load_unlocked
File "<frozen 
importlib._bootstrap_external>", line 
724, in exec_module
File "<frozen 
importlib._bootstrap_external>", line 
860, in get_code
File "<frozen 
importlib._bootstrap_external
File "<frozen 
importlib._bootstrap_external>", line 
860, in get_code
File "<frozen 
importlib._bootstrap_external>", line 
791, in source_to_code
File "<frozen importlib._bootstrap>", 
line 219, in 
_call_with_frames_removed
File "/data/data/com.termux/files/ 
home/storage/predictions/forecast/
dataflow/models.py", line 78
 home_startXI = models.JSONField()               
 home_substitutes = 
 models.JSONField()           
 away_formation = 
 models.CharField(null=True)    
 away_startXI = models.JSONField()               
 away_substitutes = 
 models.JSONField()
                                                               ^
 SyntaxError: invalid syntax

What i should do to solve this problem

This may not solve the entire issue but

from .celery import app as 
celery_app__all__ = ['celery_app']

Should read

from .celery import app as celery_app
__all__ = ('celery_app',)

I think this will help newbie like me:

You should give the full project name to workers: celery worker --app config.celery.app

Here are my observation in django.Project folder structure is

abc/
   config/
     celery.py
           - os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings")
           - app = Celery("test")
     settings.py

if you follow the same folder structure as specified in doc and task app naming convention then you can give application name(let say test) to workers celery worker --app test

abc/
  test/
     celery.py
           - os.environ.setdefault("DJANGO_SETTINGS_MODULE", "test.settings")
           - app = Celery("test")
     settings.py

then celery auto-detect test.celery.applcation

In init .py of your forecast directory add following and try to restart this might work:

    from .celery import app as celery_app
    __all__ = ('celery_app',)

I had a similar issue. You just need to check that you're in the right directory.

Run cd forecast if you're in the predictions directory.

That should workout fine because celery tries to look for celery app settings in predictions/forecast/ which doesn't exist. Your celery app is in predictions/forecast/forecast/ , that is, inside your django project directory.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM