Devang Devang - 2 months ago 27
Python Question

ImportError: Could not import settings (Is it on > sys.path? Is there an import error in the settings file?) on AWS Elastic Beanstalk

This seems to be a common problem, but still none of the answers to the replies so far have worked for me.

Django:1.6.2
Python:2.7.5

I am trying to deploy a Django application on AWS Elastic beanstalk.
My current Django project structure is-


requirements.txt
.ebextensions
|-myproject.config
myproject
|
|-init.py
|-manage.py
|-settings
|-init.py (all common settings param)
|-active.py (to select which of production or development settings to choose)
|-production.py
|-development.py
|-apps
|-polls
|-init.py
|-models.py
|-views.py
|-urls.py
|wsgi.py


My
wsgi.py
file is as follows-

import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings.active")
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings.active'

from os.path import abspath, dirname, join
from site import addsitedir

ROOT = os.path.join(os.path.dirname(__file__), '..')
sys.path.insert(0, ROOT)
PROJECT_ROOT = os.path.dirname(__file__)
sys.path.insert(0, PROJECT_ROOT)
sys.path.insert(0, abspath(join(PROJECT_ROOT, 'apps'))
sys.path.append('/opt/python/ondeck/app/myproject/')
sys.path.append('/opt/python/ondeck/app/myproject/apps')

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()


The
.ebextenstions/myproject.config
is setup as follows`

container_commands:
01_syncdb:
command: "django-admin.py syncdb --noinput"
leader_only: true
02_collectstatic:
command: "django-admin.py collectstatic --noinput"
option_settings:
- namespace: "aws:elasticbeanstalk:container:python"
option_name: WSGIPath
value: "myproject/wsgi.py"
- option_name: DJANGO_SETTINGS_MODULE
value: "settings.active"
- namespace: "aws:elasticbeanstalk:container:python:staticfiles"
option_name: "/static/"
value: "static/"


I also tried passing the
--settings=settings.active
parameter to
django-admin.py
to get the same error.
The
DJANGO_SETTINGS_MODULE
environment variable is set as
settings.module


These commands work with
manage.py


The problem is, that if from the
.ebextensions/myproject.config
file, I give a
container_command
to
django-admin.py syncdb --noinput
, I get the following error-


2014-03-12 08:16:45,988 [ERROR] Command 01_syncdb (django-admin.py
syncdb --noinput) failed 2014-03-12 08:16:45,989 [DEBUG] Command
01_syncdb output: Traceback (most recent call last):
ImportError: Could not import settings 'settings.active' (Is it on
sys.path? Is there an import error in the settings file?): No module
named settings.active


I get the same error when I try to do it locally on my development machine too. However, if on my development machine, I set the
PYTHONPATH
to the same ones as I had specified in my
wsgi.py
file, ie,

PYTHONPATH=/path/to/myproject
then the command succeeds locally. However, eventhough I have set the same path in the
wsgi.py
file, it fails on the beanstalk.

So is there a way to avoid this error without setting the PYTHONPATH?
If not, how can I set it up for AWS Elastic Beanstalk?

Answer

I fixed it by adding the project path to sys.path on the wsgi.py itself. Maybe you can try so.

Comments