I am having some trouble setting the DJANGO_SETTINGS_MODULE for my Django project.
I have a directory at ~/dev/django-project
. In this directory I have
Don't run django-admin.py
for anything other than the initial project creation. For everything after that, use manage.py
, which takes care of the finding the settings.
My favourite alternative is passing settings file as runtime parameter to manage.py
in a python package syntax, e.g:
python manage.py runserver --settings folder.filename
more info django docs
Yet another way to do deal with this issue is to use the python dotenv package and include PYTHONPATH and DJANGO_SETTINGS_MODULE in the .env file along with your other environment variables. Then modify your manage.py and wsgi.py to load them as stated in the instructions.
from dotenv import load_dotenv
load_dotenv()
On unix-like machine you can simply alias virtualenv like this and use alias instead of typing everytime:
.bashrc
alias cool='source /path_to_ve/bin/activate; export DJANGO_SETTINGS_MODULE=django_settings_folder.settings; cd path_to_django_project; export PYTHONPATH=$PYTHONPATH:$PWD'
I know there are plenty answers, but this one worked for me just for the record.
.virtual_env
folder where all the virtual environments are.export DJANGO_SETTINGS_MODULE=<django_project>.settings
or export DJANGO_SETTINGS_MODULE=<django_project>.settings.local
if you are using a separate settings file stored in a settings folder.I just encountered the same error, and eventually managed to work out what was going on (the big clue was (Is it on sys.path?)
in the ImportError
).
You need add your project directory to PYTHONPATH
— this is what the documentation means by
Note that the settings module should be on the Python import search path.
To do so, run
$ export PYTHONPATH=$PYTHONPATH:$PWD
from the ~/dev/django-project
directory before you run django-admin.py
.
You can add this command (replacing $PWD
with the actual path to your project, i.e. ~/dev/django-project
) to your virtualenv's source
script. If you choose to advance to virtualenvwrapper at some point (which is designed for this kind of situation), you can add the export PY...
line to the auto-generated postactivate
hook script.
mkdjangovirtualenv automates this even further, adding the appropriate entry to the Python path for you, but I have not tested it myself.