title: dynamically loading django apps at runtime
body: <p>Is it possible to dynamically load Django apps at runtime? Usually, apps are loaded at initialization, using the INSTALLED_APPS tuple in settings.py. However, is it possible to load additional apps at runtime? I am encountering this issue in different situations. One situation, for example, arises during testing, when I would like to dynamically load or unload apps.</p>
  <p>In order to make the problem more concrete, imagine I have a directory called <code>apps</code> where I put my apps and I would like to automatically install any new app that goes in there without manually editing the settings.py.</p>
  <p>This is easy enough. Following the example code in</p>
  <p><a href="https://stackoverflow.com/q/19833393/1009979">Django: Dynamically add apps as plugin, building urls and other settings automatically</a></p>
  <p>we put the following code in <code>settings.py</code> to could loop over the names of all sub-directories in the app directory and increment the <code>INSTALLED_APPS</code> tuple in <code>settings.py</code> like this:</p>
  <pre><code>APPS_DIR = '/path_to/apps/'
  
  for item in os.listdir(APPS_DIR):
      if os.path.isdir(os.path.join(APPS_DIR, item)):
          app_name = 'apps.%s' % item
      if app_name not in INSTALLED_APPS:
          INSTALLED_APPS += (app_name, )
  </code></pre>
  <p>After that, if I was in a Django shell, I could something like</p>
  <pre><code>from django.conf import settings
  </code></pre>
  <p>and the apps would be listed in <code>settings.INSTALLED_APPS</code>. And if I did</p>
  <pre><code>from django.core import management
  management.call_command('syncdb', interactive=False)
  </code></pre>
  <p>that would create the necessary DB tables for the apps.</p>
  <p>However, if I were to now add some more apps to the <code>apps/</code> directory, without re-starting, these would not be listed in settings.INSTALLED_APPS, and so a subsequent call to the <code>syncdb</code> would have no effect.</p>
  <p>What I would like to know is if there is something I could do --- without restarting --- to reload the settings and load/install new apps.</p>
  <p>I have tried to directly import my <code>settings.py</code>, i.e.
  <code>from myproject import settings</code></p>
  <p>and then <code>reload</code> that <code>settings</code> using the python builtin after any <code>app</code> directory changes. Although settings.INSTALLED_APPS is now changed to include the newly added apps, this ultimately makes no difference. For example,</p>
  <pre><code>from django.db import models
  models.get_apps()
  </code></pre>
  <p>shows only the original apps in <code>apps</code> and not the newly added ones and likewise</p>
  <pre><code>management.call_command('syncdb', interactive=False)
  </code></pre>
  <p>will not see the newly added apps.</p>
  <p>As I stated above, I am thinking about this situation particularly in the context of testings where I dynamically would add or remove apps.</p>
  <p>Ps. I working with Django 1.6, but on the advice of @RickyA, I see that there are some substantial changes to Django's treatment of applications in 1.7</p>
  <p><a href="https://docs.djangoproject.com/en/1.7/ref/applications/" rel="nofollow noreferrer">https://docs.djangoproject.com/en/1.7/ref/applications/</a></p>
  <p>I'm still not sure what this might mean for the problem I am facing.</p>