Django Shell No module named settings

Posted on

Question :

Django Shell No module named settings

I’ve deployed Django to Apache via mod_wsgi. Django is running fine when hosted from Apache. However, I’m trying to do some maintenance via, but when I try and run it, I get the error:

Error: Could not import settings ‘myproject.settings’ (Is it on sys.path?): No module named settings

user@localhost:~$ cd /usr/local/myproject
user@localhost:/usr/local/myproject$ ls
drwxr-xr-x 2 apache apache   4096 2011-09-07 19:38 apache
-rw-r--r-- 1 apache apache      0 2011-05-25 14:52
-rw-r--r-- 1 apache apache    813 2011-09-09 16:56
drwxr-xr-x 6 apache apache   4096 2011-09-09 16:43 myapp
-rw-r--r-- 1 apache apache   4992 2011-09-07 19:31
drwxr-xr-x 4 apache apache   4096 2011-09-08 20:32 templates
-rw-r--r-- 1 apache apache   1210 2011-09-08 14:49

Django seems to be ignoring the DJANGO_SETTINGS_MODULE environment variable.

user@localhost:~$ cd /usr/local/myproject
user@localhost:/usr/local/myproject$ export DJANGO_SETTINGS_MODULE=settings
user@localhost:/usr/local/myproject$ python shell
Error: Could not import settings 'myproject.settings' (Is it on sys.path?): No module named settings
user@localhost:/usr/local/myproject$ python
Python 2.6.5 (r265:79063, Apr 16 2010, 13:57:41) 
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import settings

Just to confirm I wasn’t going crazy, I commented out everything inside except the import settings line, and it ran correctly.

I’ve also tried setting os.environ['DJANGO_SETTINGS_MODULE'] = 'settings' and sys.path.append('/usr/local/myproject') directly at the top of, to no avail.

What’s going on here? Why is Django using the wrong settings module name? This is driving me crazy.

Asked By: Cerin


Answer #1:

This can happen if your root directory name is the same as the name of one of your apps. For example here I have a directory called bar containing a Django project with an app also called bar:

Simons-MacBook-Pro ~/temp
$ cd bar

Simons-MacBook-Pro ~/temp/bar
$ ./ shell
Error: Could not import settings 'bar.settings' (Is it on sys.path?): No module named settings

Simons-MacBook-Pro ~/temp/bar
$ ls -l
total 48
-rw-r--r--  1 simon  staff     0 25 Oct 10:46
-rw-r--r--  1 simon  staff   130 25 Oct 10:46 __init__.pyc
drwxr-xr-x  7 simon  staff   238 25 Oct 10:46 bar
-rwxr-xr-x  1 simon  staff   503 25 Oct 10:46
-rw-r--r--  1 simon  staff  5025 25 Oct 10:46
-rw-r--r--  1 simon  staff  2658 25 Oct 10:46 settings.pyc
-rw-r--r--  1 simon  staff   556 25 Oct 10:46

Changing the root directory’s name to foo (or anything else other than bar) solves the problem:

Simons-MacBook-Pro ~/temp/bar
$ cd ..

Simons-MacBook-Pro ~/temp
$ mv bar foo

Simons-MacBook-Pro ~/temp
$ cd foo

Simons-MacBook-Pro ~/temp/foo
$ ./ shell
Python 2.7.1 (r271:86832, Jun 16 2011, 16:59:05) 
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
Answered By: Simon Whitaker

Answer #2:

I had a similar problem, where the same error was being returned when I tried to run startproject myapp.
A previous answer here helped me figure it out. The problem was that I had previously pointed DJANGO_SETTINGS_MODULE to a certain file, which I had later deleted. To fix it, I just removed the pointer with this command:


Answered By: Paul Meinshausen

Answer #3:

It seems the path to your project isn’t being recognized by wsgi. This has happened to me, and to solve it I added this to the top of my .wsgi file:

import os
import sys

root_path = os.path.abspath(os.path.split(__file__)[0])
sys.path.insert(0, os.path.join(root_path, 'project_name'))
sys.path.insert(0, root_path)
Answered By: David542

Answer #4:

Somehow, if your project folder is the same name as the app that has the settings file in it, and if you have in the project root folder, it will mess wsgi. I really dont understand why but removing this file solved this for me.

Answered By: Martin Massera

Answer #5:

If you are using wsgi/uwsgi in production…

I was having the same error:

If you renamed the folder that django startproject created that has files and , check in the file the line: os.environ.setdefault("DJANGO_SETTINGS_MODULE", "<your_folder_name>.settings")

In my case i had to rename < your_folder_name> also.

Answered By: Renato Prado

Answer #6:

I had accidentally changed my DJANGO_SETTINGS_MODULE variable using the echo command: echo DJANGO_SETTINGS_MODULE=mysite.settings

I simply quit virtualenv and activated it again, which restored my settings.

Answered By: wcyn

Answer #7:

Though Simon Whitaker’s answer (that a same-name dir is confusing things) is certainly on point, rather than suggesting you change your entire extant dir structure, might I suggest:

Instead of using the “malfunctioning” / ambiguous…

import settings

…use the more specific…

from django.conf import settings
Answered By: Jan Kyu Peblik

Answer #8:

I had DJANGO_SETTINGS_MODULE set to “mealer.settings”

(django-env)ali@a-N750JV:~/snap/projects-on-django/Rester$ export -p | grep DJANGO

declare -x DJANGO_SETTINGS_MODULE="mealer.settings"

which I removed by

ali@ali-N750JV:~/snap/projects-on-django/Rester$ export -n  DJANGO_SETTINGS_MODULE

(django-env)ali@ali-N750JV:~/snap/projects-on-django/Rester$ export -p | grep DJAN


export -p | grep DJAN found nothing as you see

this answer is based on answer by Paul Meinshausen

Answered By: azamat alibaev

Leave a Reply

Your email address will not be published. Required fields are marked *