Django Shell No module settings named

I deployed Django to Apache via mod_wsgi . Django works great when hosted on Apache. However, I try to do some maintenance with manage.py , but when I try to start it, I get an error message:

Error: failed to import settings "myproject.settings" (is it on sys.path?): Not a single module with 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 __init__.py -rw-r--r-- 1 apache apache 813 2011-09-09 16:56 manage.py 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 settings.py 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 urls.py 

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 manage.py 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 that I was not crazy, I commented everything inside manage.py except the import settings line and it worked correctly.

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

What's going on here? Why is Django using the wrong settings module name? It drives me crazy.

+23
python django
Sep 09 2018-11-21T00:
source share
9 answers

This can happen if the name of your root directory matches the name of one of your applications. For example, here I have a directory called bar containing a Django project with an application 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 name to foo (or anything else but 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) >>> 
+31
Oct 25 2018-11-11T00:
source share

I had a similar problem when the same error came back when I tried to start django-admin.py startproject myapp . A previous answer here helped me figure this out. The problem was that I had previously pointed DJANGO_SETTINGS_MODULE to a specific file that I later deleted. To fix this, I simply deleted the pointer with this command:

export DJANGO_SETTINGS_MODULE=

+9
Jan 10 '14 at 7:43
source share

It seems that the path to your project is not recognized by wsgi. This 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) 
+8
Sep 09 2018-11-21T00:
source share

Anyway, if your project folder matches the name of the application in which there is a settings file, and if you have __init__.py in the root folder of the project, it will ruin wsgi. I really don't understand why, but deleting this file solved it for me.

+4
Feb 01 '17 at 2:38 on
source share

If you use wsgi / uwsgi in the production process ...

I had the same error:

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

In my case, I had to rename <your_folder_name>.

+2
Aug 26 '16 at 19:14
source share

Although Simon Whitaker answers (that the directory of the same name confuses things), it certainly stands still, instead of asking you to change the entire existing structure, can I suggest:

Instead of using "faulty" / ambiguous ...

 import settings 

... use more specific ...

 from django.conf import settings 
+2
Oct 20 '16 at 15:55
source share

Since your web application is running, make sure you use manage.py with the same python interpreter defined in your .wsgi file (and if you add other directories in sys.path to your .wsgi file, make sure that they too are in pythonpath).

If you try to import something into your settings file, which causes ImportError, Django reports that the parameters cannot be imported. In newer versions of django, (If the file settings.py does indeed exist, it causing an ImportError somehow.) , and I have come across this several times.

If this is not the case, try using django-admin.py instead, just in case something went wrong in the manage.py file. AFAIK there is no good reason to change manage.py directly.

+1
Sep 10 2018-11-11T00:
source share

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

I just exit virtualenv and activated it again, which restored my settings.

+1
Feb 11 '16 at 9:20
source share

Check compatibility between virtualenv and django versions. when it matches, it works like a gem.

0
Apr 12 '17 at 3:52 on
source share



All Articles