Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot locate modules of Django App on AWS Elastic Beanstalk

I'm trying to make a Django app run on AWS Elastic Beanstalk, but am stuck with what appears like it might be a PYTHONPATH or virtualenv problem, and I can't figure it out.

When I try to load a page in my browser, I get the yellow Django error screen (I'm still in debug mode):

Exception Value: No module named views.rest
Exception Location: /opt/python/current/app/project/urls.py in <module>, line 3
Python Executable:  /opt/python/run/venv/bin/python
Python Version: 2.7.5
Python Path:    
['/opt/python/run/venv/lib/python2.7/site-packages',
 '/opt/python/current/app',
...

My urls.py imports are:

from django.conf.urls import patterns, include, url
from django.contrib import admin
from myapp.views.rest import *

If I ssh into the EC2 instance, the path looks right; /opt/python/current/app contains my application, including /opt/python/current/app/myapp/views/rest.py.

My directory structure contains (there's more, too, of course):

app/
    .ebextensions/
        packages.config
        python.config
    .elasticbeanstalk/
        config
        optionsettings.myapp-deployment-env
    project/
        settings.py
    application.py
    myapp/
        views/
            rest.py
            views.py
            __init__.py

In __init__.py I've got:

from myapp.views.views import *
from myapp.views.rest import *

The issue is not specific to these files though - if I add another import to urls.py that fails too, so it's like it's not finding the app at all.

In packages.config I've got:

packages:
  yum:
    postgresql-devel: []

In python.config I've got (in an attempt to load the virtualenv, which might not be necessary?)

container_commands:
  00_activate:
    command: "source /opt/python/run/venv/bin/activate"
  01_make_admin_executable:
    command: "chmod +x scripts/createadmin.py"
    leader_only: true
  02_make_executable:
    command: "chmod +rx application.py"
    leader_only: true
  03_syncdb:
    command: "python manage.py syncdb --noinput"
    leader_only: true
  04_collectstatic:
    command: "python manage.py collectstatic --noinput"

option_settings:
  - option_name: AWS_SECRET_KEY
    value: mykey
  - option_name: AWS_ACCESS_KEY_ID
    value: myaccesskeyid
  - option_name: application_stage
    value: "staging"

Happy to list the other config files if they're helpful. They do include the settings location but no specific reference to PYTHONPATH etc.

[aws:elasticbeanstalk:application:environment]
DJANGO_SETTINGS_MODULE=project.settings

I've run eb update and git aws.push.

Really struggling with this one! My first attempt at an AWS deployment so have obviously missed something important. Really appreciate the help if anyone knows what I've missed.

like image 300
Paul J Avatar asked Feb 12 '23 15:02

Paul J


1 Answers

This turned out to be a mod_wsgi vs Django development server issue. This post by Graham Dumpleton was extremely helpful in understanding it (it can't be skim-read).

mod_wsgi needs to be told of the location of my site. My WSGI file (application.py) now looks like this, and the imports work:

import os
import sys

sys.path.insert(0, '/opt/python/current/app')

os.environ['DJANGO_SETTINGS_MODULE'] = 'project.settings'

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
like image 77
Paul J Avatar answered Feb 16 '23 02:02

Paul J