[Fixed]-Why do I need the DJANGO_SETTINGS_MODULE set?

11👍

Yourmanage.py is referencing an application (notifications). This forces Django to complain about DJANGO_SETTINGS_MODULE being set because the Django environment hasn’t been set up yet.

Incidentally, you can force the enviroment setup manually, but honestly I wouldn’t do this in manage.py. That’s not really a good practice in my opinion.

Here is how you can manually setup the Django environment from within any app (or program for that matter):

# set up the environment using the settings module
from django.core.management import setup_environ
from myapp import settings
setup_environ(settings)

6👍

You need to set the DJANGO_SETTINGS_MODULE environment variable because it’s how Django knows what your settings module is called (so you can have different ones per project or for testing and development.) You can set it in the scripts themselves before you import django (directly or indirectly) but that won’t do much good when you run the Django-provided scripts.

The easiest solution is probably to just set DJANGO_SETTINGS_MODULE in your shell’s startup scripts, so you won’t have to set it manually anymore. The usual files to add it to are .bash_profile and .bashrc (if you do indeed use bash.)

0👍

By default, manage.py looks for a settings module in the same directory as itself. If it doesn’t find one, it bombs out with a message to use django-admin.py instead. It doesn’t actually set up the environemnt until it runs execute_manager. If you need to run your hooks before calling your management functions, the practice I’ve seen suggested is to put them in the relevant app’s models.py.

👤jcdyer

Leave a comment