title: Can't import models in tasks.py with Celery + Django
body: <p>I want to create a background task to update a record on a specific date. I'm using Django and Celery with RabbitMQ. </p>
  
  <p>I've managed to get the task called when the model is saved with this dummy task function: </p>
  
  <p><strong>tasks.py</strong> </p>
  
  <pre><code>from __future__ import absolute_import
  from celery import Celery
  from celery.utils.log import get_task_logger
  
  logger = get_task_logger(__name__)
  
  app = Celery('tasks', broker='amqp://localhost//')
  
  
  @app.task(name='news.tasks.update_news_status')
  def update_news_status(news_id):
      # (I pass the news id and return it, nothing complicated about it)
      return news_id
  </code></pre>
  
  <p>this task is called from my save() method in my <strong>models.py</strong></p>
  
  <pre><code>from django.db import models
  from celery import current_app
  
  
  class News(models.model):
      (...)
  
      def save(self, *args, **kwargs):
  
          current_app.send_task('news.tasks.update_news_status', args=(self.id,))
  
          super(News, self).save(*args, **kwargs)
  </code></pre>
  
  <p>Thing is <strong>I want to import my News model in tasks.py</strong> but if I try to like this: </p>
  
  <p><code>from .models import News</code> </p>
  
  <p>I get this error :</p>
  
  <blockquote>
    <p>django.core.exceptions.ImproperlyConfigured: Requested setting
    DEFAULT_INDEX_TABLESPACE, but settings are not configured. You must
    either define the environment variable DJANGO_SETTINGS_MODULE or call
    settings.configure() before accessing settings.</p>
  </blockquote>
  
  <p>This is how mi <strong>celery.py</strong> looks like </p>
  
  <pre><code>from __future__ import absolute_import, unicode_literals
  from celery import Celery
  import os
  # set the default Django settings module for the 'celery' program.
  os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myapp.settings')
  app = Celery('myapp')
  
  # Using a string here means the worker doesn't have to serialize
  # the configuration object to child processes.
  # - namespace='CELERY' means all celery-related configuration keys
  #   should have a `CELERY_` prefix.
  app.config_from_object('django.conf:settings', namespace='CELERY')
  
  # Load task modules from all registered Django app configs.
  app.autodiscover_tasks()
  
  @app.task(bind=True)
  def debug_task(self):
      print('Request: {0!r}'.format(self.request))
  </code></pre>
  
  <p>I have already tried this:</p>
  
  <ol>
  <li><a href="https://stackoverflow.com/questions/36755583/cant-import-django-model-into-celery-task">can&#39;t import django model into celery task</a></li>
  <li>I have tried to make the import inside the task method <a href="https://stackoverflow.com/questions/38876482/django-and-celery-appregisterednotready-exception">Django and Celery, AppRegisteredNotReady exception</a></li>
  <li>I have also tried this <a href="https://stackoverflow.com/questions/50212082/celery-importing-models-in-tasks-py/50314466">Celery - importing models in tasks.py</a></li>
  <li>I also tried to create a utils.py and import it and was not possible.</li>
  </ol>
  
  <p>and ran into different errors but <strong>in the end I'm not able to import any module in tasks.py</strong></p>
  
  <p>There might be something wrong with my config but I can't see the error, I followed the steps in <a href="http://docs.celeryproject.org/en/latest/django/first-steps-with-django.html" rel="nofollow noreferrer">The Celery Docs: First steps with Django</a></p>
  
  <p>Also, my project structure looks like this: </p>
  
  <pre><code>├── myapp
  │   ├── __init__.py
  ├── ├── celery.py
  │   ├── settings.py
  │   ├── urls.py
  │   └── wsgi.py
  ├── news
  │   ├── __init__.py
  │   ├── admin.py
  │   ├── apps.py
  │   ├── tasks.py
  │   ├── urls.py
  │   ├── models.py
  │   ├── views.py
  ├── manage.py
  </code></pre>
  
  <p>I'm executing the worker from <code>myapp</code> directory like this: </p>
  
  <p><code>celery -A news.tasks worker --loglevel=info</code></p>
  
  <p>What am I missing here? Thanks in advance for your help!</p>
  
  <p>lambda: settings.INSTALLED_APPS</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>After making the changes suggested in comments:
  Add this to celery.py 
  <code>app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)</code></p>
  
  <p>and import inside method: tasks.py </p>
  
  <pre><code>from __future__ import absolute_import
  from celery import Celery
  from celery.utils.log import get_task_logger
  
  logger = get_task_logger(__name__)
  
  app = Celery('tasks', broker='amqp://localhost//')
  
  
  @app.task(name='news.tasks.update_news_status')
  def update_news_status(news_id):
      from .models import News
      return news_id
  </code></pre>
  
  <p>I get the following error: </p>
  
  <pre><code>[2018-07-20 12:24:29,337: ERROR/ForkPoolWorker-1] Task news.tasks.update_news_status[87f9ec92-c260-4ee9-a3bc-5f684c819f79] raised unexpected: ValueError('Attempted relative import in non-package',)
  Traceback (most recent call last):
    File "/Users/carla/Develop/App/backend/myapp-venv/lib/python2.7/site-packages/celery/app/trace.py", line 382, in trace_task
      R = retval = fun(*args, **kwargs)
    File "/Users/carla/Develop/App/backend/myapp-venv/lib/python2.7/site-packages/celery/app/trace.py", line 641, in __protected_call__
      return self.run(*args, **kwargs)
    File "/Users/carla/Develop/App/backend/news/tasks.py", line 12, in update_news_status
      from .models import News
  ValueError: Attempted relative import in non-package
  </code></pre>