Using django ORM in my celery app giving error
<p>I am developing a celery standalone application which uses Django ORM to access my database and perform operations in my data. I followed the answer to this question: <a href="http://stackoverflow.com/questions/937742/use-django-orm-as-standalone">Use Django ORM as standalone</a></p>

<p>But when I run my celery worker, it gives me the following error:</p>

<blockquote>
  <p>django.core.exceptions.ImproperlyConfigured: Requested setting
  LOGGING_CONFIG, 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>Here is my full application code and directory structure:</p>

<pre><code>standAlone/
----init.py
----settings.py

DATABASE_ENGINE    = "django.db.backends.mysql"
DATABASE_NAME      = "my_database_name"
DATABASE_USER      = "my_database_username"
DATABASE_PASSWORD  = "my_database_password"
DATABASE_HOST      = "my_host"
DATABASE_PORT      = "3306",
INSTALLED_APPS     = ("myApp")

----manage.py

#!/usr/bin/env python
import os
import sys

if __name__ == "__main__":
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myApp.settings")
    try:
        from django.core.management import execute_from_command_line
    except ImportError:
        # The above import may fail for some other reason. Ensure that the
        # issue is really that Django is missing to avoid masking other
        # exceptions on Python 2.
        try:
            import django
        except ImportError:
            raise ImportError(
                "Couldn't import Django. Are you sure it's installed and "
                "available on your PYTHONPATH environment variable? Did you "
                "forget to activate a virtual environment?"
            )
        raise
    execute_from_command_line(sys.argv)

----tasks.py

import django
django.setup()
from celery import Celery
from django.conf import settings
settings.configure(
    DATABASE_ENGINE    = "django.db.backends.mysql",
    DATABASE_NAME      = "myDatabase",
    DATABASE_USER      = "myUsername",
    DATABASE_PASSWORD  = "myPassword",
    DATABASE_HOST      = "my host",
    DATABASE_PORT      = "3306",
    INSTALLED_APPS     = ("myApp,")
)
from django.db import models
from myApp.models import *



app = Celery('tasks', broker='redis://ip_address')

@app.task(name="task1") 
def task1():
    #my task code
@app.task(name="task2") 
def task2():
    #my task code
----/myApp
--------/init.py
--------/models.py
from django.db import models 
from django.utils.encoding import python_2_unicode_compatible 
from django.utils import timezone 

# my models go here
</code></pre>

<p>Now when I run the following command:</p>

<pre><code>celery -A tasks worker &amp;
</code></pre>

<p>I get this error:</p>

<pre><code>django.core.exceptions.ImproperlyConfigured: Requested setting LOGGING_CONFIG, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
</code></pre>

<p>Can anyone suggest where I am going wrong?</p>

<p><strong>UPDATE:</strong></p>

<p>Here is the full traceback:</p>

<pre><code>Traceback (most recent call last):
  File "/usr/local/bin/celery", line 11, in &lt;module&gt;
    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 793, in execute_from_commandline
    super(CeleryCommand, self).execute_from_commandline(argv)))
  File "/usr/local/lib/python2.7/dist-packages/celery/bin/base.py", line 309, in execute_from_commandline
    argv = self.setup_app_from_commandline(argv)
  File "/usr/local/lib/python2.7/dist-packages/celery/bin/base.py", line 469, in setup_app_from_commandline
    self.app = self.find_app(app)
  File "/usr/local/lib/python2.7/dist-packages/celery/bin/base.py", line 489, in find_app
    return find_app(app, symbol_by_name=self.symbol_by_name)
  File "/usr/local/lib/python2.7/dist-packages/celery/app/utils.py", line 235, in find_app
    sym = symbol_by_name(app, imp=imp)
  File "/usr/local/lib/python2.7/dist-packages/celery/bin/base.py", line 492, in symbol_by_name
    return symbol_by_name(name, imp=imp)
  File "/usr/local/lib/python2.7/dist-packages/kombu/utils/__init__.py", line 96, in symbol_by_name
    module = imp(module_name, package=package, **kwargs)
  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/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/root/standAlone/tasks.py", line 2, in &lt;module&gt;
    django.setup()
  File "/usr/local/lib/python2.7/dist-packages/django/__init__.py", line 22, in setup
    configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
  File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 53, in __getattr__
    self._setup(name)
  File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 39, in _setup
    % (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting LOGGING_CONFIG, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
</code></pre>

<p><strong>UPDATE 2:</strong></p>

<p>Now that I have applied the answer provided, it is giving me error saying</p>

<blockquote>
  <p>no module named m</p>
</blockquote>

<p>with the following traceback:</p>

<pre><code>Traceback (most recent call last):
  File "/usr/local/bin/celery", line 11, in &lt;module&gt;
    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 793, in execute_from_commandline
    super(CeleryCommand, self).execute_from_commandline(argv)))
  File "/usr/local/lib/python2.7/dist-packages/celery/bin/base.py", line 309, in execute_from_commandline
    argv = self.setup_app_from_commandline(argv)
  File "/usr/local/lib/python2.7/dist-packages/celery/bin/base.py", line 469, in setup_app_from_commandline
    self.app = self.find_app(app)
  File "/usr/local/lib/python2.7/dist-packages/celery/bin/base.py", line 489, in find_app
    return find_app(app, symbol_by_name=self.symbol_by_name)
  File "/usr/local/lib/python2.7/dist-packages/celery/app/utils.py", line 235, in find_app
    sym = symbol_by_name(app, imp=imp)
  File "/usr/local/lib/python2.7/dist-packages/celery/bin/base.py", line 492, in symbol_by_name
    return symbol_by_name(name, imp=imp)
  File "/usr/local/lib/python2.7/dist-packages/kombu/utils/__init__.py", line 96, in symbol_by_name
    module = imp(module_name, package=package, **kwargs)
  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/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/root/standAlone/tasks.py", line 13, in &lt;module&gt;
    django.setup()
  File "/usr/local/lib/python2.7/dist-packages/django/__init__.py", line 27, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/usr/local/lib/python2.7/dist-packages/django/apps/registry.py", line 85, in populate
    app_config = AppConfig.create(entry)
  File "/usr/local/lib/python2.7/dist-packages/django/apps/config.py", line 90, in create
    module = import_module(entry)
  File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
ImportError: No module named m
</code></pre>

<p><strong>UPDATE 3</strong></p>

<p>It turns out that I needed to change </p>

<pre><code>INSTALLED_APPS     = ("myApp")
</code></pre>

<p>to </p>

<pre><code>INSTALLED_APPS     = ("myApp",)
</code></pre>

<p>and everything works good then!</p>

<p>You need to configure your settings before you call <code>django.setup()</code>. Simply switching the statements should fix the issue:</p>

<pre><code># tasks.py
import django
from celery import Celery
from django.conf import settings


settings.configure(
    DATABASE_ENGINE    = "django.db.backends.mysql",
    DATABASE_NAME      = "myDatabase",
    DATABASE_USER      = "myUsername",
    DATABASE_PASSWORD  = "myPassword",
    DATABASE_HOST      = "my host",
    DATABASE_PORT      = "3306",
    INSTALLED_APPS     = ("myApp",)  # Move the comma out of the quotes
)
django.setup()
</code></pre>

python django django-models orm