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>If you're creating any script that is using your django project, it is absolutely necessary to set path to settings of your project <strong>before</strong> any import from django or your project. And you're importing user model from django in 1st line and model from your project in second.</p>

<p>Also, you will need to call <code>django.setup()</code> first.</p>

<p>To fix that, move <code>import os</code> and setting path to django settings to the very beginning of your script, and put django.setup() just after that (with proper import), like this:</p>

<pre><code># first, set path to project settings
import os
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'DjangoWebProject.settings')
import django
django.setup()

# now you can import anything else
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>

python django