Rico
Rico

Reputation: 6032

Unable to execute Django runserver and no suggested solution seems to work

I've run Django servers on localhost before and have never run into this problem. I'm desperately trying to figure out what I've done wrong.

I'm using Django 1.4 with Python 2.7 on Ubuntu 12.04.

As far as I can tell I've configured everything correctly - I'm actually using another functional Django project I built as a go-by.

If I run the following command (or any recommended variation thereof) I receive an error.

django-admin.py runserver localhost:8000

Here is the error:

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

Can someone please enlighten me as to why this error is occurring, how to fix it and why it doesn't happen with my other Django project?!?

I've found many posts regarding this problem just by doing some quick Google searches, but none of the suggested solutions have helped - nor do I truly understand them.

Upvotes: 0

Views: 310

Answers (2)

jdotjdot
jdotjdot

Reputation: 17042

Providing some more code or examples of your directory structure might help.

First, the command is generally manage.py runserver 8000, so try that, and that make might a difference.

Second, in Django 1.4, the location of the settings.py file was moved. In previous versions of Django, the directory structure looked like this:

myproject/
    settings.py
    views.py
    urls.py
    myapp/
        models.py
        ...
    ...

However, in Django 1.4, the main project settings and files were moved to a different directory:

myproject/
    myproject/
        settings.py
        views.py
        urls.py
    myapp/
        models.py
        urls.py
        ...
    ...

So if you're using Django 1.4 but going off of previous examples, your settings.py might be in the wrong place. Additionally, I've found that when running django-admin.py startproject, it sometimes incorrectly creates two settings.py files, once in the old location and one in the new, which could be additionally confusing you. The only one that manage.py would pay attention to is the one in the project's directory.

If it turns out that your settings.py is in the wrong place but you don't want to move it, as your error suggests, you could set an environmental variable called DJANGO_SETTINGS_MODULE as the path to the Django settings.py you'd like to use for your project. I definitely don't recommend doing this.

Upvotes: 1

Anuj Gupta
Anuj Gupta

Reputation: 10526

I'm pretty sure you're supposed to run

manage.py runserver

from inside your project directory. It automatically loads your settings.py, etc.

From the Django docs:

Generally, when working on a single Django project, it’s easier to use manage.py. Use django-admin.py with DJANGO_SETTINGS_MODULE, or the --settings command line option, if you need to switch between multiple Django settings files

Upvotes: 4

Related Questions