How do i set django setting to a file which is outside the django tree?
<p>I have this python file tasks.py</p>

<pre><code>import os
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'DjangoWebProject.settings')
    from django.contrib.auth.models import User
    from logreg.models import ActivationCode
    import datetime

    def remove_users():
        print 'hello worldddddddddddddddddddddddddddddddddd'
        inactive_users = []
        activation_codes = ActivationCode.objects.all()
        for activation_code in activation_codes:
            if datetime.datetime.date(activation_code.key_expires) &lt; datetime.datetime.date(datetime.datetime.now()):
                inactive_users.append(activation_code.user_id)

        for inactive_user in inactive_users:
            User.objects.filter(id=inactive_user).delete()
</code></pre>

<p>But this is in the root folder and when i try to execute it, it gives me the following error</p>

<blockquote>
  <p>File
  "C:\Users\deybala1\AppData\Local\Continuum\Anaconda2\lib\site-packages\dj
  ango\apps\registry.py", line 124, in check_apps_ready
      raise AppRegistryNotReady("Apps aren't loaded yet.") django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.</p>
</blockquote>

<p>How do i fix this?</p>

<p>Note that you're trying to add a settings module inside a script that already requires it. </p>

<p>Wouldn't it be easier if you add a specific django command? Thanks to it you'd be able to start your task with <code>python manage.py --settings=&lt;path_to_your_settings&gt;</code>.</p>

<p>Another tip:</p>

<p>Move every django import statement below </p>

<pre><code>os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'DjangoWebProject.settings')
</code></pre>

python django