How to fix Django "ModuleNotFoundError: No module named 'application'" on AWS?
Asked Answered
T

6

9

I am trying to redeploy a Django web application on AWS. My elastic beanstalk environment has been red a couple of times. When I ran eb logs on the cli, I am getting a "ModuleNotFoundError: No module named 'application' error". I think this has got to do with my wsgi configuration.

I have deployed this web app on AWS before. I messed up when I tried deploying a new version then decided to just start over. Here is my wsgi.py configuration:

```import os

from django.core.wsgi import get_wsgi_application
from django.contrib.staticfiles.handlers import StaticFilesHandler

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")

application = StaticFilesHandler(get_wsgi_application())```

When I deploy the app, it's giving me a 502: Bad gateway error. Let me know if you would like more info on the issue. Any pointers would be greatly appreciated.

Thermonuclear answered 1/6, 2021 at 13:0 Comment(1)
Did you find any solution for this? I've been stuck at this for 2 days straight. Any advice would be very much appreciated!Overline
N
1

I faced this issue as well. In my case I was getting a 502 nginx error after deploying to AWS EB using eb deploy and my environment had a red flag.

My AWS EB was using Amazon Linux 2 AMI, everything was set correctly BUT after lot of tries and errors I realized my .ebextensions folder was named .ebtextensions.

I wish EB or django would have indicated on its logs that my folder was nanmed incorrectly.

Nearsighted answered 14/10, 2022 at 12:31 Comment(0)
M
5

By default, Elastic Beanstalk looks for a file named application.py to start your application. Because this doesn't exist in the Django project that you've created, you need to make some adjustments to your application's environment. You also must set environment variables so that your application's modules can be loaded.

follow these instructions: https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create-deploy-python-django.html#python-django-configure-for-eb

I got this error because in an older version the django.config looked like this:

option_settings:
  aws:elasticbeanstalk:container:python:
    WSGIPath: ebdjango/wsgi.py

and now it should look like this:

option_settings:
  aws:elasticbeanstalk:container:python:
    WSGIPath: ebdjango.wsgi:application
Mosely answered 6/6, 2021 at 12:43 Comment(0)
F
5

I solved this problem by

  1. Ensuring my django.config file had the correct settings e.g
    option_settings:
      aws:elasticbeanstalk:container:python:
      WSGIPath: <app name>.wsgi:application
  1. By including a .ebignore file in my project.

https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/eb-cli3-configuration.html#eb-cli3-ebignore

Fichu answered 5/3, 2023 at 22:14 Comment(3)
After spending an entire day on this... all i needed was a empty .ebignore file :|, i need sleepFichu
I ran into the same; Bruh, 2 days of banging my head against the wall. THANK YOU!Dominoes
Including .ebignore is a workaround, not a fix, and may have unexpected side effects. From the docs: "When .ebignore is present, the EB CLI doesn't use git commands to create your source bundle. This means that EB CLI ignores files specified in .ebignore, and includes all other files. In particular, it includes uncommitted source files."Symmetrize
N
1

I faced this issue as well. In my case I was getting a 502 nginx error after deploying to AWS EB using eb deploy and my environment had a red flag.

My AWS EB was using Amazon Linux 2 AMI, everything was set correctly BUT after lot of tries and errors I realized my .ebextensions folder was named .ebtextensions.

I wish EB or django would have indicated on its logs that my folder was nanmed incorrectly.

Nearsighted answered 14/10, 2022 at 12:31 Comment(0)
G
1

Also be sure to git commit your .ebextensions folder with the django.config file before eb deploy.

Gasper answered 10/2 at 20:27 Comment(0)
V
0

I got this error and also spent days trying to solve it. The problem is from the django.config file.

option_settings:
  aws:elasticbeanstalk:container:python:
  WSGIPath: <app name>.wsgi:application

The first value (app name) in the WSGIPath option needs to be the directory that contains the wsgi.py file

Varmint answered 31/12, 2022 at 2:30 Comment(1)
Hello, Do you have any idea how to get to the directory? I would need to do : WSGIPath: <foldername>/<app name>.wsgi:application but after trying a gazillion options it does not want to navigate to the directoryFancied
V
0

Check the WSGIPath in elastic beanstalk configuration (Software Category) in the AWS console too.

It should be

<app_name>.wsgi:application
Verse answered 20/1, 2023 at 1:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.