Devang
Devang

Reputation: 1541

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?

Upvotes: 3

Views: 5331

Answers (3)

Zakdigital
Zakdigital

Reputation: 46

Even though I was also able to solve the settings import problem with Andres answer and the follow up comments by Unoti -->

To use the wsgi as produced by django (in my case 1.9), add this to the wsgi file:

sys.path.insert(0, os.path.abspath(os.path.dirname(os.path.dirname(__file__))))

I found this to be an interesting read that still seems relevant:

Secondly, that wsgi.py was auto generated, it gave users the impression that it would always be correct, must be used and wouldn't need modifying. Consequence was that people stopped seeing what one of the main roles of the WSGi script file was for, which was to set environment variables before the Django application got loaded. ...from: https://gist.github.com/GrahamDumpleton/b380652b768e81a7f60c

2. So since we are no longer assuming that the location and contents of the wsgi file are set in stone for production another option would be to copy your wsgi file outside of the project and simply change the settings module path address setting.

ps. I will mention, even though this probably won't work for you since you don't have access to an apache configuration (?), but (for those who do,) you can use the 'python-path' configuration via:

WSGIDaemonProcess example.com python-home=/path/to/venv python-path=/path/to/mysite.com

Upvotes: 0

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

Upvotes: 4

Evan Brown
Evan Brown

Reputation: 382

Try modifying the DJANGO_SETTINGS_MODULE value in wsgi.py and .ebextenstions/myproject.config to be myproject.settings.active.

Upvotes: 0

Related Questions