ImportError: Could not import settings
Asked Answered
C

6

36

I am trying to develop an sample project in Django and getting errors when I run the syncdb command.

This is how my project structure looks like:

/Users/django_demo/godjango/bookings:

manage.py
    registration/
        forms.py
        views.py
        models.py
        urls.py
    bookings/
        settings.py
        urls.py

And my manage.py file is as follows:

#!/usr/bin/env python
import os
import sys

if __name__ == "__main__":
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "bookings.settings")
    from django.core.management import execute_from_command_line
    execute_from_command_line(sys.argv)

And my PYTHONPATH and DJANGO_SETTINGS_MODULE are set as below

$echo $DJANGO_SETTINGS_MODULE
bookings.settings

$ echo $PYTHONPATH
/Users/django_demo/godjango/bookings

And my WSGI.py file looks like below:

 import os
 os.environ.setdefault("DJANGO_SETTINGS_MODULE", "bookings.settings")
 from django.core.wsgi import get_wsgi_application
 application = get_wsgi_application()

When I run the python manage.py syncdb command, I am getting the following error.

Traceback (most recent call last):
  File "manage.py", line 11, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/django_demo/godjango/lib/python2.7/site-packages/django/core/management/__init__.py", line 443, in execute_from_command_line
    utility.execute()
  File "/Users/django_demo/godjango/lib/python2.7/site-packages/django/core/management/__init__.py", line 382, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/django_demo/godjango/lib/python2.7/site-packages/django/core/management/__init__.py", line 261, in fetch_command
    klass = load_command_class(app_name, subcommand)
  File "/Users/django_demo/godjango/lib/python2.7/site-packages/django/core/management/__init__.py", line 69, in load_command_class
    module = import_module('%s.management.commands.%s' % (app_name, name))
  File "/Users/django_demo/godjango/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module
    __import__(name)
  File "/Users/django_demo/godjango/lib/python2.7/site-packages/django/core/management/commands/syncdb.py", line 8, in <module>
    from django.core.management.sql import custom_sql_for_model, emit_post_sync_signal
  File "/Users/django_demo/godjango/lib/python2.7/site-packages/django/core/management/sql.py", line 6, in <module>
    from django.db import models
  File "/Users/django_demo/godjango/lib/python2.7/site-packages/django/db/__init__.py", line 11, in <module>
    if DEFAULT_DB_ALIAS not in settings.DATABASES:
  File "/Users/django_demo/godjango/lib/python2.7/site-packages/django/utils/functional.py", line 184, in inner
    self._setup()
  File "/Users/django_demo/godjango/lib/python2.7/site-packages/django/conf/__init__.py", line 42, in _setup
    self._wrapped = Settings(settings_module)
  File "/Users/django_demo/godjango/lib/python2.7/site-packages/django/conf/__init__.py", line 95, in __init__
    raise ImportError("Could not import settings '%s' (Is it on sys.path?): %s" % (self.SETTINGS_MODULE, e))
ImportError: Could not import settings 'bookings.settings' (Is it on sys.path?): No module named unipath

Could someone suggest what I am missing?

Creatine answered 20/10, 2012 at 10:48 Comment(0)
W
34

The error says ImportError: Could not import settings 'bookings.settings' (Is it on sys.path?): No module named unipath

So, is your path /Users/django_demo/godjango/bookings within the python-sys.path?

Check it in your shell with:

$ python
Python 2.7.3 (v2.7.3:70274d53c1dd, Apr  9 2012, 20:52:43) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> for path in sys.path: print path
... 

/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/suds-0.4-py2.7.egg
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/MySQL_python-1.2.3-py2.7-macosx-10.6-intel.egg
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/PIL-1.1.7-py2.7-macosx-10.6-intel.egg
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/spyne-2.8.2_rc-py2.7.egg
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/lxml-3.0alpha2-py2.7-macosx-10.6-intel.egg
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pytz-2012d-py2.7.egg
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django_debug_toolbar-0.9.4-py2.7.egg
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django_social_auth-0.7.7-py2.7.egg
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/python_openid-2.2.5-py2.7.egg
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/oauth2-1.5.211-py2.7.egg
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/Django-1.4.1-py2.7.egg
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/httplib2-0.7.6-py2.7.egg
/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages
/Library/Python/2.7/site-packages
>>> 

If not - you need to add it or simply move your bookings app into one of the paths represented in your sys.path

Wits answered 20/10, 2012 at 12:10 Comment(3)
How can I "move" my app into one of the paths?Manmade
@Timo: if you use virtualenv, you can use the command add2virtualenvEicher
please , any one can help me this matter. I also face like this error. #33890777 still , couldn't resolve.Containment
M
14

Modify your wsgi.py file from

 import os
 os.environ.setdefault("DJANGO_SETTINGS_MODULE", "bookings.settings")
 from django.core.wsgi import get_wsgi_application
 application = get_wsgi_application()

to

import os, sys    
sys.path.append(' /Users/Sreek/django_demo/godjango/bookings')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "bookings.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
Metacarpal answered 14/2, 2013 at 14:2 Comment(1)
This is where my issue was.Hemocyte
N
11

The significant part of the traceback here is right at the very end. It says "No module named unipath". You've referred to that somewhere in your code, but you don't seem to have it in your project - it's not part of the standard library, so you'll need to install it somewhere that Python can see it.

Necklace answered 20/10, 2012 at 12:15 Comment(0)
A
4

Alternatively, you can even pass the settings path at run time like so:

python manage.py syncdb --settings=bookings.settings --pythonpath=/Users/django_demo/godjango/bookings

This should override the environment variable (DJANGO_SETTINGS_MODULE)

Albumen answered 19/4, 2016 at 18:26 Comment(1)
Aww yeah buddy ! You saved me pow(2, 64) hours ! Thank you !Germain
L
1

Review hour /etc/apache2/httpd.conf file; you must include the WSGIPythonPath directive, to indicate the folder which contains your Django project (manage.py file), like:

WSGIPythonPath /home/user/Projects/Django/MyProject

also if you used some weird port in your VirtualHost, specify if for listening:

Listen 90

Hope this helps somebody

Loudermilk answered 16/9, 2013 at 20:34 Comment(1)
If it's in the PYTHONPATH, then I don't think this is necessary, is it?Sim
M
1

ImportError: Could not import settings 'settings' (Is it on sys.path? Is there an import error in the settings file?): No module named setting

In my case I was using __init__.py to import other files and there was an error in it, so settings might actually exist but be flawed.

Marna answered 31/3, 2014 at 17:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.