title: ModuleNotFoundError: No module named Foo - How can I import a model into a Django app's script?
body: <p>My Django skill level: <strong>noob</strong>.</p>
  
  <p>I am going nuts at setting the <code>DJANGO_SETTINGS_MODULE</code> properly to finally get my model imported within a script. I use a <code>virtualenv</code> for my project.</p>
  
  <p><strong>This is my current error:</strong></p>
  
  <p><code>ModuleNotFoundError: No module named 'dashex'</code></p>
  
  <p>And the according <code>feeder.py</code> script:</p>
  
  <pre><code>import os
  import sys
  os.environ['DJANGO_SETTINGS_MODULE'] = 'dashex.settings'
  sys.path.append('os.path.dirname(os.path.dirname(os.path.abspath(__file__)))')
  import django
  django.setup()
  import zmq
  import time
  from time import sleep
  import uuid
  from Dashboard_app.models import AccountInformation
  [...]
  </code></pre>
  
  <p><strong>Background:</strong></p>
  
  <p>I first used the <code>settings.configure()</code> solution posted <a href="https://stackoverflow.com/questions/59152742/atom-script-add-on-doesnt-recognize-django-model-settings-when-running-a-scri">here</a>. However, this solution raised <a href="https://stackoverflow.com/questions/59157787/while-true-loop-results-in-recursionerror-because-of-django-setup">this</a> error... So I hope <code>DJANGO_SETTINGS_MODULE</code> can do the trick.</p>
  
  <p>According to the documentation (<a href="https://docs.djangoproject.com/en/2.2/topics/settings/" rel="nofollow noreferrer">docu</a>) I have to use <code>Django-admin</code> to set <code>DJANGO_SETTINGS_MODULE</code>.</p>
  
  <p><strong>What I've tried:</strong></p>
  
  <ul>
  <li><p>Executed <br> <code>$ set DJANGO_SETTINGS_MODULE=dashex.settings</code> in my command prompt on windows, just nothing happens. <br></p></li>
  <li><p>Executed <br>
  <code>$ py manage.py set DJANGO_SETTINGS_MODULE=dashex.settings</code> which raises the error <code>unknown command set</code></p></li>
  <li><p>Executed <br> <code>$ django-admin set DJANGO_SETTINGS_MODULE=dashex.settings</code> in the <code>manage.py</code> root of my project raises the error <code>no module named dashex</code>.</p></li>
  <li><p><a href="https://stackoverflow.com/questions/12854271/setting-django-settings-module-on-windows-correctly">This</a> answer from @Casper suggests to do the above mentioned, but how to handle this: <br><code>C:\temp\testproject&gt; set DJANGO_SETTINGS_MODULE=testproject.settings</code> ?</p></li>
  <li><p><a href="https://stackoverflow.com/questions/8826534/how-can-i-correctly-set-django-settings-module-for-my-django-project-i-am-using">This</a> solution from @Max suggests to append <code>export DJANGO_SETTINGS_MODULE=&lt;django_project&gt;.settings</code> to my virtual-environment. What is meant with this, how to do that?</p></li>
  <li><p>Executed <br> <code>$ django-admin runserver --settings=dashex.settings</code> which also raises the error <code>no module named dashex</code>.</p></li>
  <li><p>Executed <br> <code>$ py manage.py runserver --settings=dashex.settings</code> which starts the server with the proper settings. <strong>But the initial mentioned error persists.</strong></p></li>
  </ul>
  
  <p><a href="https://i.stack.imgur.com/J4EZ8.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/J4EZ8.jpg" alt="enter image description here"></a></p>
  
  <p><strong>My project:</strong></p>
  
  <p><a href="https://i.stack.imgur.com/5iQ4r.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/5iQ4r.jpg" alt="enter image description here"></a></p>
  
  <p><strong>Settings.py:</strong></p>
  
  <pre><code>import os.path
  import os
  
  BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
  
  
  # Static files (CSS, JavaScript, Images)
  # https://docs.djangoproject.com/en/2.2/howto/static-files/
  
  
  STATIC_ROOT = ''
  STATIC_URL = '/static/'
  STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]
  
  
  # Quick-start development settings - unsuitable for production
  # See https://docs.djangoproject.com/en/2.2/howto/deployment/checklist/
  
  # SECURITY WARNING: keep the secret key used in production secret!
  SECRET_KEY = ''
  
  # SECURITY WARNING: don't run with debug turned on in production!
  DEBUG = True
  
  ALLOWED_HOSTS = ["127.0.0.1", "locahost"]
  
  
  # Application definition
  
  INSTALLED_APPS = [
      'django.contrib.admin',
      'django.contrib.auth',
      'django.contrib.contenttypes',
      'django.contrib.sessions',
      'django.contrib.messages',
      'django.contrib.staticfiles',
      'django.contrib.sites',
      #other Apps
      'Wiki_app',
      'rest_framework',
      'Dashboard_app.apps.DashboardAppConfig'
  ]
  </code></pre>
  
  <p><strong>Latest traceback after @alasdair's solution:</strong></p>
  
  <pre><code>Traceback (most recent call last):
    File "C:\Users\Jonas Blickle\Desktop\dashex\Dashboard_app\feeder.py", line 6, in &lt;module&gt;
      django.setup()
    File "C:\Program Files\lib\site-packages\django\__init__.py", line 19, in setup
      configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
    File "C:\Program Files\lib\site-packages\django\conf\__init__.py", line 79, in __getattr__
      self._setup(name)
    File "C:\Program Files\lib\site-packages\django\conf\__init__.py", line 66, in _setup
      self._wrapped = Settings(settings_module)
    File "C:\Program Files\lib\site-packages\django\conf\__init__.py", line 157, in __init__
      mod = importlib.import_module(self.SETTINGS_MODULE)
    File "C:\Program Files\lib\importlib\__init__.py", line 127, in import_module
      return _bootstrap._gcd_import(name[level:], package, level)
    File "&lt;frozen importlib._bootstrap&gt;", line 1014, in _gcd_import
    File "&lt;frozen importlib._bootstrap&gt;", line 991, in _find_and_load
    File "&lt;frozen importlib._bootstrap&gt;", line 961, in _find_and_load_unlocked
    File "&lt;frozen importlib._bootstrap&gt;", line 219, in _call_with_frames_removed
    File "&lt;frozen importlib._bootstrap&gt;", line 1014, in _gcd_import
    File "&lt;frozen importlib._bootstrap&gt;", line 991, in _find_and_load
    File "&lt;frozen importlib._bootstrap&gt;", line 973, in _find_and_load_unlocked
  ModuleNotFoundError: No module named 'dashex'
  [Finished in 0.37s]
  </code></pre>
  
  <p><strong>Working solution:</strong></p>
  
  <pre><code>import sys
  sys.path.insert(0, r"C:\Users\Jonas\Desktop\Dashex")
  import os
  os.environ['DJANGO_SETTINGS_MODULE'] = 'Dashex.settings'
  import django
  django.setup()
  from Dashboard.models import AccountInformation
  [...]
  </code></pre>