[Django]-Why am I getting this error in Django?

17👍

I don’t believe I am supposed to
manually create an “export DJANGO…”
environment variable…

Manually or otherwise, you are supposed to ensure that variable is in the environment before you import a Django models file — not sure what the causes are for your disbelief, but, whatever they may be, that disbelief is ill-founded.

38👍

You need to properly import your settings file. If you don’t import the django environment variables first, your import will fail as you’ve noticed. You need to code something like:

import os

# Set the DJANGO_SETTINGS_MODULE environment variable.
os.environ['DJANGO_SETTINGS_MODULE'] = "myproject.settings"

#do your stuff

14👍

from command line ran “python manage.py shell” should include the right settings for you and give you the python prompt.

👤Ian

7👍

One alternative to messing about with the environment is to just write your script as a Django custom management command, which takes care of setting up the environment for you (along with other things like option parsing, etc).

0👍

From command line, please execute this command. This may help you:

export DJANGO_SETTINGS_MODULE=projectname.settings

0👍

I had this similar error, and I realized that I forgot to create the file “init.py” in to the app folder:

myapp:
    manage.py
    myapp:
        __init__.py
        settings.py
        ....

Leave a comment