ModuleNotFoundError: No module named Foo – How can I import a model into a Django app’s script?

My Django skill level: noob.

I am going nuts at setting the DJANGO_SETTINGS_MODULE properly to finally get my model imported within a script. I use a virtualenv for my project.

This is my current error:

ModuleNotFoundError: No module named 'dashex'

And the according script:

import os
import sys
os.environ['DJANGO_SETTINGS_MODULE'] = 'dashex.settings'
import django
import zmq
import time
from time import sleep
import uuid
from Dashboard_app.models import AccountInformation


I first used the settings.configure() solution posted here. However, this solution raised this error… So I hope DJANGO_SETTINGS_MODULE can do the trick.

According to the documentation (docu) I have to use Django-admin to set DJANGO_SETTINGS_MODULE.

What I’ve tried:

  • Executed
    $ set DJANGO_SETTINGS_MODULE=dashex.settings in my command prompt on windows, just nothing happens.

  • Executed
    $ py set DJANGO_SETTINGS_MODULE=dashex.settings which raises the error unknown command set

  • Executed
    $ django-admin set DJANGO_SETTINGS_MODULE=dashex.settings in the root of my project raises the error no module named dashex.

  • This answer from @Casper suggests to do the above mentioned, but how to handle this:
    C:\temp\testproject> set DJANGO_SETTINGS_MODULE=testproject.settings ?

  • This solution from @Max suggests to append export DJANGO_SETTINGS_MODULE=<django_project>.settings to my virtual-environment. What is meant with this, how to do that?

  • Executed
    $ django-admin runserver --settings=dashex.settings which also raises the error no module named dashex.

  • Executed
    $ py runserver --settings=dashex.settings which starts the server with the proper settings. But the initial mentioned error persists.

My project:

import os.path
import os

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

# Static files (CSS, JavaScript, Images)

STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]

# Quick-start development settings - unsuitable for production
# See

# SECURITY WARNING: keep the secret key used in production secret!

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = ["", "locahost"]

# Application definition

    #other Apps

Latest traceback after @alasdair’s solution:

Traceback (most recent call last):
  File "C:\Users\Jonas Blickle\Desktop\dashex\Dashboard_app\", line 6, in <module>
  File "C:\Program Files\lib\site-packages\django\", line 19, in setup
    configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
  File "C:\Program Files\lib\site-packages\django\conf\", line 79, in __getattr__
  File "C:\Program Files\lib\site-packages\django\conf\", line 66, in _setup
    self._wrapped = Settings(settings_module)
  File "C:\Program Files\lib\site-packages\django\conf\", line 157, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "C:\Program Files\lib\importlib\", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 961, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 973, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'dashex'
[Finished in 0.37s]

Working solution:

import sys
sys.path.insert(0, r"C:\Users\Jonas\Desktop\Dashex")
import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'Dashex.settings'
import django
from Dashboard.models import AccountInformation

Leave a Reply

Your email address will not be published. Required fields are marked *