42
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
$ ./manage.py 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 __init__.py
-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 manage.py
-rw-r--r-- 1 simon staff 5025 25 Oct 10:46 settings.py
-rw-r--r-- 1 simon staff 2658 25 Oct 10:46 settings.pyc
-rw-r--r-- 1 simon staff 556 25 Oct 10:46 urls.py
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
$ ./manage.py 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.
(InteractiveConsole)
>>>
17
I had a similar problem, where the same error was being returned when I tried to run
django-admin.py 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:
export DJANGO_SETTINGS_MODULE=
- [Django]-Django load block for css
- [Django]-Django – No such table: main.auth_user__old
- [Django]-Django Admin – Overriding the widget of a custom form field
9
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)
- [Django]-Django South – table already exists
- [Django]-Django filter queryset __in for *every* item in list
- [Django]-How to create a custom decorator in Django?
6
Somehow, if your project folder is the same name as the app that has the settings file in it, and if you have __init__.py
in the project root folder, it will mess wsgi. I really dont understand why but removing this file solved this for me.
- [Django]-Django – makemigrations – No changes detected
- [Django]-Get list item dynamically in django templates
- [Django]-Django template, if tag based on current URL value
5
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
- [Django]-Django character set with MySQL weirdness
- [Django]-Disable button after submit with jQuery
- [Django]-In Django, can you add a method to querysets?
4
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 setting.py files and wsgi.py , check in the wsgi.py file the line: os.environ.setdefault("DJANGO_SETTINGS_MODULE", "<your_folder_name>.settings")
In my case i had to rename < your_folder_name> also.
- [Django]-Django static files versioning
- [Django]-Nested inlines in the Django admin?
- [Django]-Get absolute file path of FileField of a model instance in Django
3
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.
- [Django]-Django – enforcing ManyToManyField unique items
- [Django]-How to remove all relations from manytomany?
- [Django]-AttributeError: 'module' object has no attribute 'setdefaultencoding'
2
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
(django-env)ali@ali-N750JV:
export -p | grep DJAN found nothing as you see
this answer is based on answer by Paul Meinshausen
- [Django]-Django: how to get format date in views?
- [Django]-Django development server, how to stop it when it run in background?
- [Django]-How to break "for loop" after 1 iteration in Django template
2
my answer might not match the question exactly, but I have to point out.
one reason might be, the directory lack the __init__.py
file!
This is my environment files:
[root@hpc-proxy apigateway]# ls -al gateway/gateway/settings/
total 52
drwxr-xr-x 2 root root 228 Feb 19 18:19 .
drwxr-xr-x 3 root root 177 Feb 19 19:00 ..
-rw-r--r-- 1 root root 7738 Feb 19 17:52 base.py
-rw-r--r-- 1 root root 6722 Feb 19 18:19 base.pyc
-rw-r--r-- 1 root root 2539 Feb 19 15:02 shanghai07.py
-rw-r--r-- 1 root root 950 Feb 5 12:45 shanghai07root.py
-rw-r--r-- 1 root root 2828 Feb 5 13:00 testenv.py
and I always get the error message like this:
ImportError: No module named shanghai07
So when I created the __init__.py
file in the ‘settings’ directory,
the error went away!
I hope my answer can help some beginners.
- [Django]-Django admin and MongoDB, possible at all?
- [Django]-Right way to return proxy model instance from a base model instance in Django?
- [Django]-Django query get last n records
1
Since your web app is working, check that you’re running manage.py
with the same python interpreter that’s defined in your .wsgi file (and if you append other directories to sys.path in your .wsgi file, make sure they’re in the pythonpath here too).
If you try to import something in your settings file that throws an ImportError, Django tells you settings cannot be imported. Newer versions of django will mention (If the file settings.py does indeed exist, it's causing an ImportError somehow.)
and I’ve run into this a few times.
If it’s not that, maybe try using django-admin.py instead, just in case something has gone wrong in your manage.py file. AFAIK there is no good reason to modify manage.py directly.
- [Django]-How can I filter a Django query with a list of values?
- [Django]-Token Authentication for RESTful API: should the token be periodically changed?
- [Django]-Organizing Django unit tests
0
Please check for compatibility between the virtualenv version and the django version. when it matches, it works like a gem.
- [Django]-How to add Check Constraints for Django Model fields?
- [Django]-Using IntellijIdea within an existing virtualenv
- [Django]-How do I invalidate @cached_property in django
0
In my case the wsgi.py file was working when the system was running normally, but I was getting the ImportError when trying to do a manual manage.py command like migrate or collectstatic.
I checked wsgi.py for the way it imports the settings and noticed that it first adds the settings path to the sys.path as follows:
import sys
sys.path.append('/opt/server/settings')
I added that to the top of the manage.py and it works.
- [Django]-Django Queryset with filtering on reverse foreign key
- [Django]-Pass request context to serializer from Viewset in Django Rest Framework
- [Django]-Django column "name" of relation "django_content_type" does not exist
0
For me it was actually the PATH, so the project wasn’t inside the path, I did this:
export PYTHONPATH=/var/www/project:$PYTHONPATH
If it’s a local project it might be something like this:
export PYTHONPATH=~/my-project:$PYTHONPATH
Questions? comment and I’ll answer.
- [Django]-Upload image available at public URL to S3 using boto
- [Django]-Django storages: Import Error – no module named storages
- [Django]-OperationalError, no such column. Django
0
I tried all the suggestions in the answers above without joy.
My error was the startup command line:
python manage.py runserver --settings=settings 192.168.1.183:8006
When I added the directory where the settings file was located, joy…
python manage.py runserver --settings=<settings_file_dir>.settings 192.168.1.183:8006
The DJANGO_SETTINGS_MODULE variable made no difference
- [Django]-Create Django model or update if exists
- [Django]-Django: "projects" vs "apps"
- [Django]-How django time zone works with model.field's auto_now_add