47

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 a virtual environment which I have set up with virtualenv, and also a django project called "blossom" with an app within it called "onora". Running tree -L 3 from ~/dev/django-project/ shows me the following:

.
├── Procfile
├── blossom
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── fixtures
│   │   └── initial_data_test.yaml
│   ├── manage.py
│   ├── onora
│   │   ├── __init__.py
│   │   ├── __init__.pyc
│   │   ├── admin.py
│   │   ├── admin.pyc
│   │   ├── models.py
│   │   ├── models.pyc
│   │   ├── tests.py
│   │   └── views.py
│   ├── settings.py
│   ├── settings.pyc
│   ├── sqlite3-database
│   ├── urls.py
│   └── urls.pyc
├── blossom-sqlite3-db2
├── requirements.txt
└── virtual_environment
    ├── bin
    │   ├── activate
    │   ├── activate.csh
    │   ├── activate.fish
    │   ├── activate_this.py
    │   ├── django-admin.py
    │   ├── easy_install
    │   ├── easy_install-2.7
    │   ├── gunicorn
    │   ├── gunicorn_django
    │   ├── gunicorn_paster
    │   ├── pip
    │   ├── pip-2.7
    │   ├── python
    │   └── python2.7 -> python
    ├── include
    │   └── python2.7 -> /System/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7
    └── lib
        └── python2.7

I am trying to dump my data from the database with the command

django-admin.py dumpdata

My approach is to run cd ~/dev/django-project and then run source virtual_environment/bin/activate and then run django-admin.py dumpdata

However, I am getting the following error:

ImportError: Settings cannot be imported, because environment variable DJANGO_SETTINGS_MODULE is undefined.

I did some googling and found this page: https://docs.djangoproject.com/en/dev/topics/settings/#designating-the-settings

which tell me that

When you use Django, you have to tell it which settings you're using. Do this by using an environment variable, DJANGO_SETTINGS_MODULE. The value of DJANGO_SETTINGS_MODULE should be in Python path syntax, e.g. mysite.settings. Note that the settings module should be on the Python import search path.

Following a suggestion at Setting DJANGO_SETTINGS_MODULE under virtualenv? I appended the lines

export DJANGO_SETTINGS_MODULE="blossom.settings"
echo $DJANGO_SETTINGS_MODULE

to virtual_environment/bin/activate. Now, when I run the activate command in order to activate the virtual environment, I get output reading:

DJANGO_SETTINGS_MODULE set to blossom.settings

This looks good to me, but now the problem I have is that running

django-admin.py dumpdata

returns the following error:

ImportError: Could not import settings 'blossom.settings' (Is it on sys.path?): No module named blossom.settings

What am I doing wrong? How can I check thesys.path? How is this supposed to work?

Thanks.

Community
  • 1
  • 1
Deonomo
  • 1,583
  • 3
  • 16
  • 25

8 Answers8

69

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.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • 1
    Thanks -- `python blossom/manage.py dumpdata` worked as expected. – Deonomo Jan 11 '12 at 21:13
  • 1
    True. Then why django docs are saying "Use exactly one of either configure() or DJANGO_SETTINGS_MODULE. Not both, and **not neither**." ? https://docs.djangoproject.com/en/dev/topics/settings/ – lajarre Oct 16 '12 at 20:16
  • 8
    Current thinking — as featured in the ["DeployDjango" site](http://www.deploydjango.com/django_project_structure/index.html) and the recent *[Two Scoops of Django](http://django.2scoops.org/)* book — is that `django-admin.py` with separated settings functions perfectly well, and is actually preferable to using `manage.py`. – supervacuo Jan 23 '13 at 21:58
  • 1
    But, but.. but.. what about [dumpdata](https://docs.djangoproject.com/en/1.11/ref/django-admin/#dumpdata). I need that in my life. – Paul Samsotha Sep 08 '17 at 14:28
29

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.

supervacuo
  • 9,072
  • 2
  • 44
  • 61
  • Dirty. But working and useful. You're encouraging circumventing problems. If something is not on sys.path, this means it needs to be packaged and installed, not hacked onto sys.path. I know, I'm solving one problem, creating two more. –  Nov 09 '15 at 14:22
  • Interesting: how do you think an in-development Django project should be added to `sys.path`, if not by `manage.py`, `django-admin.py`'s `--pythonpath` option, or the `PYTHONPATH` env var (none of which seems "dirtier" than the others..) – supervacuo Nov 09 '15 at 17:30
  • 1
    Package it (create setup.py) and install via pip inestall [-e]. Have a look at longer explenation: https://gist.github.com/bartekbrak/42d59fd43a9af14a0cc5 –  Nov 14 '15 at 15:19
19

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'

zzart
  • 11,207
  • 5
  • 52
  • 47
6

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

stelios
  • 2,679
  • 5
  • 31
  • 41
2

I know there are plenty answers, but this one worked for me just for the record.

  1. Navigate to your .virtual_env folder where all the virtual environments are.
  2. Go to the environment folder specific to your project
  3. Append 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.
Eje
  • 354
  • 4
  • 8
Max
  • 982
  • 10
  • 21
0

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()
aris
  • 22,725
  • 1
  • 29
  • 33
0

I had similar error while working on windows machine. My problem was using wrong debug configuration. Use Python:django as your debug config option. First ensure you've exported/set django_settings_module correctly here.

enter image description here

7guyo
  • 3,047
  • 1
  • 30
  • 31
0

I ran into this issue when changing out my virtualenv environment (stupid idea, btw). I tried setting DJANGO_SETTINGS_MODULE and it didn't work. My fix, instead of starting my server with:

django-admin runserver

I now run my server with this command:

python manage.py runserver

No other changes needed!