Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django cannot import LOCAL settings

EDIT: Playing around with Django 1.7... and Python 3

I can't seem to import local_settings.py into my settings.py file when using manage.py.

If I execute the settings.py file directly, my local_settings.py file is imported fine, without any errors.

However, when I run manage.py, it complains that it could not find the local_settings.py module. settings.py and local_settings.py are in the same folder...

Any ideas?

Traceback (most recent call last):
  File "/home/cg/webdev/riv_com/lib/python3.4/site-packages/django/conf/__init__.py", line 94, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "/usr/lib/python3.4/importlib/__init__.py", line 104, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 2231, in _gcd_import
  File "<frozen importlib._bootstrap>", line 2214, in _find_and_load
  File "<frozen importlib._bootstrap>", line 2203, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 1129, in _exec
  File "<frozen importlib._bootstrap>", line 1448, in exec_module
  File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
  File "/home/cg/webdev/riv_com/riv_com/riv_com/settings.py", line 79, in <module>
    from local_settings import *
ImportError: No module named 'local_settings'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "./manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/home/cg/webdev/riv_com/lib/python3.4/site-packages/django/core/management/__init__.py", line 427, in execute_from_command_line
    utility.execute()
  File "/home/cg/webdev/riv_com/lib/python3.4/site-packages/django/core/management/__init__.py", line 386, in execute
    settings.INSTALLED_APPS
  File "/home/cg/webdev/riv_com/lib/python3.4/site-packages/django/conf/__init__.py", line 46, in __getattr__
    self._setup(name)
  File "/home/cg/webdev/riv_com/lib/python3.4/site-packages/django/conf/__init__.py", line 42, in _setup
    self._wrapped = Settings(settings_module)
  File "/home/cg/webdev/riv_com/lib/python3.4/site-packages/django/conf/__init__.py", line 98, in __init__
    % (self.SETTINGS_MODULE, e)
ImportError: Could not import settings 'riv_com.settings' (Is it on sys.path? Is there an import error in the settings file?): No module named 'local_settings'

EDIT:

This error seems to only occur in Python3.4

I've tested with:

Django 1.6 + Python3.4 = error
Django 1.7 + Python3.4 = error

Django 1.6 + Python2.7 = okay

like image 591
Brownbay Avatar asked Apr 07 '14 05:04

Brownbay


2 Answers

Python 3.4 does not support implicit relative imports: from local_settings import * in Python 3 is an absolute import and would only search for a local_settings module in your sys.path, but NOT in the same directory where your settings.py module is. You need to use explicit relative import: from .local_settings import *; this would also work in Python 2.7.

Reference: PEP 328

like image 188
lanzz Avatar answered Oct 12 '22 00:10

lanzz


In settings.py

import os
SITE_ROOT = os.path.realpath(os.path.dirname(__file__))

DEBUG = False
# delete TEMPLATE_DEBUG = DEBUG

at the bottom of settings.py

##################
# LOCAL SETTINGS #
##################

# Allow any settings to be defined in local_settings.py which should be
# ignored in your version control system allowing for settings to be
# defined per machine.
try:
    from local_settings import *
except ImportError:
    pass

In loacal_settings.py

# Grabs the site root setup in settings.py
# import os
# from settings import SITE_ROOT

from settings import *

DEBUG = True
TEMPLATE_DEBUG = DEBUG

# sqlite is the quick an easy development db
DATABASES = {
  'default': {
      'ENGINE': 'django.db.backends.sqlite3',
      'NAME': os.path.join(SITE_ROOT, 'djlocal.db'),
      'USER': '',             # Not used with sqlite3.
      'PASSWORD': '',         # Not used with sqlite3.
      'HOST': '',             # Not used with sqlite3.
      'PORT': '',             # Not used with sqlite3.
  }
}
like image 21
4dgaurav Avatar answered Oct 11 '22 22:10

4dgaurav