Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I correctly set DJANGO_SETTINGS_MODULE for my Django project (I am using virtualenv)?

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.

like image 979
Deonomo Avatar asked Jan 11 '12 20:01

Deonomo


People also ask

Where do I set 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.

How do I set call settings in Django?

Designating the settings 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 .

What does Django setup () do?

It is used if you run your Django app as standalone. It will load your settings and populate Django's application registry. You can read the detail on the Django documentation.


Video Answer


2 Answers

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.

like image 167
Daniel Roseman Avatar answered Sep 21 '22 15:09

Daniel Roseman


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.

like image 30
supervacuo Avatar answered Sep 21 '22 15:09

supervacuo